python - 通过click()找到超链接后如何使用find_element

标签 python selenium selenium-webdriver web-scraping

我用过

continue_link=driver.find_elements_by_partial_link_text("contract")

获取链接列表。如何在已有的 Web 元素上应用 find_elements?

以及如何返回网页上的字符串? find_element 可以做到这一点吗?

例如:

      <div class="article-detail-text">
         <h1>notice</h1>
         <p class="article-date">release date:2019-05-22</p>

       <p>hi:<br />
  I like basketball and football.<br />
  I like cooking.<br />
  Thanks.</p>

我想返回: “我喜欢篮球和足球。我喜欢 cooking 。谢谢。”

最佳答案

我相信正确的XPath expression匹配您提供的 HTML 片段将类似于:

//div[@class='article-detail-text']/descendant::p[contains(text(),'hi')]

找到相关的 <div> tag 后您将能够获得 descendant 的文本 <p> tag使用innerText property

print(driver
      .find_element_by_xpath("//div[@class='article-detail-text']/descendant::p[contains(text(),'hi')]")
      .get_attribute("innerText"))

演示:

enter image description here

关于python - 通过click()找到超链接后如何使用find_element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56947427/

相关文章:

selenium - 提交按钮在 Selenium webdriver 中不起作用

java - 如何在 webdriver(selenium2.0) 的情况下单击链接

java - 如何在 Java 中使用 Selenium 提交表单

python - Pandas:跨数据帧的列使用 DataFrame.apply() 和 Series.sum() 函数比调用 DataFrame.sum() 更快。为什么?

python - 如何在 Django 中创建自动递增整数字段?

python - Convert_to_r_dataframe 给出错误 no attribute dtype

python - 将一系列数字转换为唯一的、随机出现的字符串

javascript - 如何使用 webdriver (JavaScript) 在 selenium 中停止测试?

python - 使用 Python 和 Selenium 进行网页抓取

python-3.x - selenium.common.exceptions.WebDriverException : Message: unknown error: unable to discover open pages using ChromeDriver through Selenium