简体   繁体   中英

selecting text of a page in selenium webdriver python

I'm a relative beginner in python and selenium and am facing an issue. Using Selenium, I click on a link that by default opens in a new tab and I want to print all the text of the new page in my termianal, How do I accomplish this? please share your code. Attaching a snippet of the page source.

<html><head><script src="chrome-extension://mooikfkahbdckldjjndioackbalphokd/assets/prompt.js"></script></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">                                   
using namespace std;
int main() {
    int n;
    cin >> n;
    return 0;
}
</pre>
</body>
</html>

I need to print the code enclosed in the 'pre' tag in my terminal.

As you say if your page only contains HTML that you have provided, you can do the following.

from selenium import webdriver

driver= webdriver.Chrome("chromedriverr")

driver.get("your_test_site_url")

print(driver.find_element_by_xpath("/html/body/pre").text)

When you run the program you get following output in terminal.

using namespace std;
int main() {
    int n;
    cin >> n;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM