Python 硒 - 找不到这样的元素...我迷路了
Python Selenium - No Such Element Found... I'm Lost
我试图点击一个应该将我带到下一页的元素,但是,我似乎无法正确找到它,甚至无法找到页面上的任何项目。
HTML 元素代码
<a data-qa="menu-button"
class="HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"
data-cv-test="headerSearchLink"
href="/cars"
target="_self">Search Cars
</a>
Visual of what I'm trying to click on
我的代码不起作用(我有正确的导入语句)
PATH = "my_path_to_chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.carvana.com")
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
link.click()
我尝试使用这种方法按 class 名称进行搜索,但只收到此错误消息
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"}
(Session info: chrome=xx.x.xxxx.xxx)
我也尝试过使用 CSS 选择器进行搜索。我什至做了一点完整性检查并在网站上搜索了 html 代码的第一行,出于某种原因它给了我同样的错误。有什么帮助...谢谢!
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
这段代码给 return 一个节点列表,你不能点击它,你可以尝试获取第一个元素,所以这里是代码:
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
或者再见,在导航器的控制台中使用 js 执行此操作:
document.getElementByNames('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
问题是 class 名称中有空格。 class 名称函数对此不起作用,我建议尝试
driver.find_element_by_css_selector('.HeaderMenu__HeaderItemButton-kr6p0e-0.hbeYmr.MenuButton__MenuButtonWrapper-dq0g44-0.imLDTG')
我试图点击一个应该将我带到下一页的元素,但是,我似乎无法正确找到它,甚至无法找到页面上的任何项目。
HTML 元素代码
<a data-qa="menu-button"
class="HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"
data-cv-test="headerSearchLink"
href="/cars"
target="_self">Search Cars
</a>
Visual of what I'm trying to click on
我的代码不起作用(我有正确的导入语句)
PATH = "my_path_to_chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.carvana.com")
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
link.click()
我尝试使用这种方法按 class 名称进行搜索,但只收到此错误消息
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"}
(Session info: chrome=xx.x.xxxx.xxx)
我也尝试过使用 CSS 选择器进行搜索。我什至做了一点完整性检查并在网站上搜索了 html 代码的第一行,出于某种原因它给了我同样的错误。有什么帮助...谢谢!
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
这段代码给 return 一个节点列表,你不能点击它,你可以尝试获取第一个元素,所以这里是代码:
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
或者再见,在导航器的控制台中使用 js 执行此操作:
document.getElementByNames('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
问题是 class 名称中有空格。 class 名称函数对此不起作用,我建议尝试
driver.find_element_by_css_selector('.HeaderMenu__HeaderItemButton-kr6p0e-0.hbeYmr.MenuButton__MenuButtonWrapper-dq0g44-0.imLDTG')