如何修复Selenium WebDrive中的'NoSuchElementExecptionError'

时间:2019-04-04 04:14:12

标签: python selenium selenium-webdriver

我对Selenium还是陌生的,我正在尝试使代码单击正确的google网络搜索链接。如果我为((driver.find_element_by_link_text))添加或删除s仍然不起作用,当它生效时,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()

1 个答案:

答案 0 :(得分:0)

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