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...

问题:

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.


回答1:

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;
}
  • 发表于 2019-12-25 16:16
  • 阅读 ( 190 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除