【问题标题】:My selenium program fails to find element我的硒程序找不到元素
【发布时间】:2019-06-27 19:01:23
【问题描述】:

我正在尝试在 chrome 中使用 python 和 selenium 执行 Web 自动化。 事情是我试图找到一个没有 id 或类名的按钮。

xpath 是:

 //*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]

而html代码是

<span class="SectionMethod" onclick="window.location.href=&quot;explorer/explorer.aspx?root=user&quot;;" style="cursor:pointer;text-decoration:underline;color:CadetBlue;">Open</span>

这是一个名为 open 的按钮,但还有其他类似的按钮具有相同的文本和类,因此我无法通过文本定位。

这是我的代码:

from selenium import webdriver


driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())

driver.get("..............") 


driver.find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]')

这是我得到的错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Form1"}
  (Session info: chrome=75.0.3770.100)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.16299 x86_64).

【问题讨论】:

  • 确保使用预期条件等到表单加载完毕。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

您可能正在寻找一个尚未加载的元素。根据the example from the documentation

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

在你的情况下:

EC.presence_of_element_located((By.ID, "myDynamicElement"))

EC.presence_of_element_located((By.XPATH, '//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]'))

如果这不能解决错误,我建议您查看how to form a MCVEhow to ask a well received question(建议在创建新帐户时阅读)。然后将您的问题编辑成更简洁的格式,以便我们更有效地帮助您!欢迎使用 StackOverflow。

【讨论】:

    【解决方案2】:

    你正确的 xpath 是:

    //span[@class='SectionMethod' and text() = 'Open']
    

    【讨论】:

    • 它给出了这个错误 NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='SectionMethod' and text( ) = 'Open']"} (会话信息: chrome=75.0.3770.100) (驱动程序信息: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0。 16299 x86_64)
    • 尝试使用此 Xpath 并等待,如下面的 Reedinationer 推荐的那样。
    【解决方案3】:

    大概您正尝试在 &lt;span&gt; 元素上使用文本为 Openclick() 并为实现这一目标,您必须为 element_to_be_clickable() 诱导 WebDriverWait 和您可以使用以下任一解决方案:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.SectionMethod[onclick*='explorer/explorer']"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='SectionMethod' and contains(@onclick,'explorer/explorer')][text()='Open']"))).click()
      
    • 注意:您必须添加以下导入:

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

    此外,当您使用chrome=75.0.3770.100 时,您需要将ChromeDriver 更新为ChromeDriver 75.0.3770.90 (2019-06-13)

    【讨论】:

    • 尝试时提示超时异常
    猜你喜欢
    • 2017-09-14
    • 2021-08-17
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多