【问题标题】:Python Selenium can't find XPathPython Selenium 找不到 XPath
【发布时间】:2021-04-25 03:03:12
【问题描述】:

我需要找到文本的 XPath(你能找到我吗?):

尝试了大约 10 分钟,但无法做到。

【问题讨论】:

  • 试试//*[text(), "Can you find me?")
  • 您需要获取文本还是仅获取元素?
  • @vitaliis 我需要文本(你能找到我吗?)
  • @CodeIt 我之前尝试过,但它没有以某种方式工作?您还有其他建议吗?

标签: python selenium web-scraping xpath


【解决方案1】:

你有 id,使用它。

driver.find_element_by_id("modal-root-header").text

如果 id 由于某种原因不是唯一的,请使用 xpath:

driver.find_element_by_xpath("//h4[@id='modal-root-header']").text

如果.text 不起作用,请尝试.get_attribute("value").get_attribute("innerText")

【讨论】:

  • 不起作用 'driver.find_element_by_xpath("//h4[@id='modal-root-header']").text' 没有属性 '.text'
  • 然后将 html 代码添加到您的问题中。另外,尝试我添加的另一个选项。
  • 如果你能发布一个更好的 html 表示你看到的,这是可能的。
  • 谢谢我有点笨,你的代码可以工作,但我忘了把它存储在一个变量中,我现在使用的代码是 element = browser.find_element_by_xpath("//h4[@id='modal -root-header']").text 并检查 elements == "你能找到我吗?"感谢并抱歉浪费您的时间:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多