如何修复Selenium WebDri中的NoSuchElementExecptionError

2024-03-29 06:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Selenium非常陌生,我正在尝试让代码点击正确的googleweb搜索链接。如果我添加或删除以下项的s:(driver.find\元素\按\链接\文本)它仍然不起作用,当它起作用时,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()

Tags: fromimportsendbytime链接driverselenium
1条回答
网友
1楼 · 发布于 2024-03-29 06:46:52

DOM中不存在链接文本“维拉诺瓦学院-国王城”。 目前的文本是“维拉诺瓦大学国王城”没有连字符。 另外,您要查找的元素不可见。你知道吗

早期错误:

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"}

以下是“维拉诺瓦大学国王城”生成的错误日志:

>>> 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)

相关问题 更多 >