【问题标题】:Find button element using Selenium/Python使用 Selenium/Python 查找按钮元素
【发布时间】:2020-06-06 14:24:35
【问题描述】:

我正在尝试在下一页上找到一个按钮 - https://www.dukascopy.com/swiss/english/marketwatch/sentiment/

The button I am trying to locate is the Liquidity providers button

我尝试在 driver.find_element_by_xpath(...) 中同时使用 xpath 和完整 xpath,但它似乎根本找不到按钮。我也尝试了所有其他 find_element 方法,但无济于事。

我在使用 Xpath 时遇到以下错误

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=":1"]"}
(Session info: chrome=83.0.4103.97)

任何帮助将不胜感激。谢谢

【问题讨论】:

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

首先,它根本不是一个按钮。这是一个div。其次,它在 iframe 中。 iframe 或框架内的元素不存在于主顶层文档中。您必须切换才能看到它们。这样的事情会起作用。

# get the iframe element
iframe = driver.get_element_by_tag_name('iframe')
# switch to it
driver.switch_to_frame(iframe)
# with the new frame active locate the element
liquidity_customers = driver.find_element_by_xpath("//*[text()='Liquidity consumers']").click()
# switch back to top level of document
driver.switch_to_default_content()

【讨论】:

  • 没问题。看起来这些按钮只是排序,您可能不需要在 iframe 中单击它们一次,看起来所有数据都可用,而无需单击任何按钮。
【解决方案2】:

查看页面唯一使用“流动性提供者”的地方是在您想要的区域,所以,

driver.find_elements_by_xpath("//*[contains(text(), 'Liquidity providers')]")

注意:这将返回一个元素列表,您需要列表中的唯一一个。

【讨论】:

  • 嗨,它确实找到了一个元素,但它似乎不是我想要的实际按钮/div,因为它再次产生错误。错误:selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素 ... 在点 (394, 11) 不可点击
猜你喜欢
  • 1970-01-01
  • 2016-11-30
  • 2022-01-19
  • 2023-01-22
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2016-06-08
相关资源
最近更新 更多