首页 > 解决方案 > 如何在 selenium python 中修复这个 xpath?

问题描述

我对这个 python 和 selenium 世界很陌生。尝试使用 selenium webdriver 用 python 自动化一些无聊的东西。我在xpath下面遇到问题。似乎"="把事情搞砸了。

有人可以帮忙吗?

L = driver.find_element_by_xpath("//*[@id="ext-comp-1016"]/div[1]/table/tbody/tr/td[2]/div[1]").click()

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ext-comp-1016"]/div[1]/table/tbody/tr/td[2]/div[1]"}
  (Session info: chrome=87.0.4280.66)

标签: python-3.xseleniumxpath

解决方案


  1. 检查 xpath 是否在 DOM 页面上正常工作。

  2. 如果 xpath 是正确的,我建议使用隐式等待方法。可能运行的脚本比 DOM 页面加载更快。

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    L = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.Xpath,"/div[1]/table/tbody/tr/td[2]/div[1]"))).click()
    

推荐阅读