Selenium returns NoSuchElementException 错误

Selenium returns NoSuchElementException Error

我是 python 的新手。 最近对网络爬虫产生了兴趣

今天我陷入了 NoSuchElementException

这是我要抓取的网页。

当我点击我删除的用户名时,它 returns 框是这样的。

虽然我使用了从 Chrome 开发者工具复制的 xpath, 它 returns 我 NoSuchElementException:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main-area"]/div[4]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a"}
  (Session info: chrome=87.0.4280.88)

HTML是这样的

<a href="#" class="m-tcol-c" onclick="ui(event, 'royaltina',3,'이주연마인','25868806','me', 'false', 'true', 'schoolch', 'false', '5'); return false;">이주연마인</a>

我的代码就是这样,

driver.find_element_by_xpath("//*[@id=\"main-area\"]/div[4]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a")

我检查过有这个 xpath,但是当我将它放入 .find_element_by_xpath() 方法时它 returns 错误。

我确实分享了网页,但需要登录才能到达那里, 所以我不能分享网页。

您能猜出是什么导致了这个问题吗?

我检查了时间是不是问题。 我检查了iframe是不是问题。

提前致谢 祝你有美好的一天!

要定位文本为 이주연마인 的元素,您需要归纳 WebDriverWait for the element_to_be_clickable() and you can use either of the following :

  • 使用LINK_TEXT:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "이주연마인")))
    
  • 使用CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.m-tcol-c[onclick*='royaltina']")))
    
  • 使用XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='m-tcol-c' and contains(@onclick, 'royaltina')][text()='이주연마인']")))
    
  • 注意:您必须添加以下导入:

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

参考资料

您可以在 中找到一些相关讨论: