【问题标题】:selecting text of a page in selenium webdriver python在 selenium webdriver python 中选择页面的文本
【发布时间】:2020-04-13 16:45:41
【问题描述】:

我是 python 和 selenium 的相对初学者,我正面临一个问题。 使用 Selenium,我单击默认情况下在新选项卡中打开的链接,我想在终端中打印新页面的所有文本,我该如何完成?请分享您的代码。 附加页面源的sn-p。

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

我需要在我的终端中打印包含在“pre”标签中的代码。

【问题讨论】:

  • 欢迎来到 SO 为了让人们回答您的问题,请提供一个最小的、可重复的示例,请参阅stackoverflow.com/help/minimal-reproducible-example 的帮助指南
  • 尝试使用request-html
  • 请提供完整的 html 以帮助重现您的问题
  • @TekNath 这是整个html,网页只显示用户提交的c++代码。

标签: python html selenium web-scraping automation


【解决方案1】:

如您所说,如果您的页面仅包含您提供的 HTML,您可以执行以下操作。

from selenium import webdriver

driver= webdriver.Chrome("chromedriverr")

driver.get("your_test_site_url")

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

当您运行程序时,您会在终端中获得以下输出。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多