【问题标题】:How to fix 'NoSuchElementExecptionError' in Selenium WebDrive [duplicate]如何修复 Selenium WebDriver 中的“NoSuchElementException”[重复]
【发布时间】:2019-08-25 17:24:47
【问题描述】:

我对 Selenium 很陌生,我正在尝试让代码点击正确的谷歌网络搜索链接。如果我添加或删除 s for:(driver.find_element_by_link_text) 它仍然不起作用,并且当它起作用时, click() 不起作用。我只能让两者中的一个同时工作。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path='Python\chromedriver.exe')
driver.get('https://www.google.com/')

element = driver.find_element_by_name('q')
element.send_keys('Villanova')
element.send_keys(Keys.ENTER)

element = driver.find_element_by_link_text('Villanova College - King City').click()


time.sleep(5)
driver.close()

【问题讨论】:

  • 你从不使用你的 import NoSuchElementException 可以从你的帖子中删除吗?
  • 欢迎来到 SO。 find_element_by_link_text 将返回一个元素,而find_elements_by_link_text 将返回一个元素列表,因此您在使用find_elements_by_link_text 时无法单击。但是,如果在尝试单击之前可以找到该文本,请检查 chrome 开发工具。还要考虑同步,意味着让结果加载然后找到元素。
  • 这是您得到的错误吗:selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"link text","selector":"维拉诺瓦学院 - 国王城"}
  • Google 搜索结果页面中没有 linkText 作为 Villanova College - King City

标签: python selenium selenium-webdriver


【解决方案1】:

DOM 中不存在链接文本“Villanova College - King City”。 出现的文本是没有连字符的“villanova College King City”。 您要查找的元素也不可见。

之前的错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Villanova College - King City"}

以下是使用“villanova university king city”生成的错误日志:

>>> driver.find_element_by_xpath("//div[text()='villanova college king city']").click()
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    driver.find_element_by_xpath("//div[text()='villanova college king city']").click()
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=72.0.3626.121)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)

【讨论】:

  • 我更改了 x 路径,但我不断收到点击功能的错误消息。
  • 您现在可能遇到的错误是元素不可见异常?
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2015-12-23
  • 2015-04-24
相关资源
最近更新 更多