【问题标题】:Selenium: NoSuchElementException even after using explicit WaitSelenium: NoSuchElementException 即使在使用显式等待之后
【发布时间】:2016-12-19 07:33:17
【问题描述】:

我正在http://ntry.com/#/stats/ladder/round.php 上找到一个元素, 但在尝试了几种方法后,我一直找不到它,包括 ind_element_by_css_selector、ind_element_by_xpath...等等。

即使我使用了 WebDriverWait,我仍然失败。会有什么问题?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

driver = webdriver.Firefox()
driver.get("http://ntry.com/#/stats/ladder/round.php")

try:
   element = EC.presence_of_element_located((By.XPATH, '//div[@id="analysis-table"]/div[1]/div[1]/p[1]/span[1]/strong'))

   #or element = WebDriverWait(driver, 30).until(
       EC.presence_of_element_located((By.CSS_SELECTOR, "#analysis-table>div.bar_graph>div:nth-child(1)>p.left.on>span.per>strong"))
   )
except:
   print "HIJUDE"

driver.quit()

我使用了隐式等待,但这也会产生同样的错误。 不使用 Wait 也会导致 NoSuchElementException。

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":'//div[@id="analysis-table"]/div[1]/div[1]/p[1]/span[1]/strong'}

网站是否与找不到元素有关?还是使用 Xpath 或 css_selector 以外的方法?我很困惑为什么会这样。

----------编辑-------------- 我发现在 div[@id="analysis-table"] 的上层 xpath 级别有 iframe。我想这就是原因。我应该总是使用 driver.switch_to_frame() 在这种情况下?顺便说一句,driver.switch_to_window() 与 frame() 不同吗?

【问题讨论】:

  • p.left.on 上的 .on 是什么?我没有看到“开”课...
  • 我发现类名有时会变得不同,因为这个网站是动态网页。但使用上面的 Xpath 似乎总是有效的。
  • 是的,如果你有一个iframe 元素,你需要切换到框架,然后它应该能够找到该元素。
  • 是的,使用 driver.switch_to_frame() 切换到 iframe 有效。谢谢!

标签: python selenium


【解决方案1】:

路径中的p.left.on 有问题。一个元素有一个类left,另一个有一个类righton。应该是

"#analysis-table>div.bar_graph>div:nth-child(1)>p.left>span.per>strong"

或者

"#analysis-table>div.bar_graph>div:nth-child(1)>p.right.on>span.per>strong"

【讨论】:

  • 感谢您的回答。我想原因是因为
猜你喜欢
  • 2021-05-26
  • 1970-01-01
  • 2021-12-18
  • 2012-05-11
  • 2021-01-19
  • 1970-01-01
  • 2023-03-11
  • 2018-01-04
相关资源
最近更新 更多