我目前使用的是最新版本的Selenium,似乎有一个问题,Selenium在我没有使用quit方法的情况下意外关闭。下面是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get(url="https://en.wikipedia.org/wiki/Main_Page")
article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
article_count.click()
它在关闭之前打开浏览器并给我以下错误:
Stacktrace:
Backtrace:
GetHandleVerifier [0x00007FF62C894A62+57106]
(No symbol) [0x00007FF62C80CF52]
(No symbol) [0x00007FF62C6DE2CB]
(No symbol) [0x00007FF62C71786E]
(No symbol) [0x00007FF62C71795C]
(No symbol) [0x00007FF62C750477]
(No symbol) [0x00007FF62C7369FF]
(No symbol) [0x00007FF62C74E522]
(No symbol) [0x00007FF62C736793]
(No symbol) [0x00007FF62C70CE81]
(No symbol) [0x00007FF62C70E064]
GetHandleVerifier [0x00007FF62CB44222+2873042]
GetHandleVerifier [0x00007FF62CB96590+3209792]
GetHandleVerifier [0x00007FF62CB8F3AF+3180639]
GetHandleVerifier [0x00007FF62C925F25+652245]
(No symbol) [0x00007FF62C818618]
(No symbol) [0x00007FF62C8147C4]
(No symbol) [0x00007FF62C8148BC]
(No symbol) [0x00007FF62C804C33]
BaseThreadInitThunk [0x00007FFF274026AD+29]
RtlUserThreadStart [0x00007FFF2894AA68+40]
有人能帮我一下吗?(注意我使用的是PyCharm)
**编辑:**终端中的输出完整输出是这样的:
Traceback (most recent call last):
File "C:\Users\lyuan\Desktop\100_days_of_code\day48\main.py", line 7, in <module>
article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
self.error_handler.check_response(response)
File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, 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":"#articalcount a"}
(Session info: chrome=115.0.5790.171); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
GetHandleVerifier [0x00007FF7BB4B4A62+57106]
(No symbol) [0x00007FF7BB42CF52]
(No symbol) [0x00007FF7BB2FE2CB]
(No symbol) [0x00007FF7BB33786E]
(No symbol) [0x00007FF7BB33795C]
(No symbol) [0x00007FF7BB370477]
(No symbol) [0x00007FF7BB3569FF]
(No symbol) [0x00007FF7BB36E522]
(No symbol) [0x00007FF7BB356793]
(No symbol) [0x00007FF7BB32CE81]
(No symbol) [0x00007FF7BB32E064]
GetHandleVerifier [0x00007FF7BB764222+2873042]
GetHandleVerifier [0x00007FF7BB7B6590+3209792]
GetHandleVerifier [0x00007FF7BB7AF3AF+3180639]
GetHandleVerifier [0x00007FF7BB545F25+652245]
(No symbol) [0x00007FF7BB438618]
(No symbol) [0x00007FF7BB4347C4]
(No symbol) [0x00007FF7BB4348BC]
(No symbol) [0x00007FF7BB424C33]
BaseThreadInitThunk [0x00007FFD436A26AD+29]
RtlUserThreadStart [0x00007FFD4484AA68+40]
1条答案
按热度按时间qyzbxkaa1#
此错误消息...
...意味着使用您使用的locator strategy在HTML DOM中没有立即识别出任何元素。因此,浏览器被关闭。
解决方案
要点击 article count,理想情况下,您需要为element_to_be_clickable()引入WebDriverWait,您可以使用以下locator strategies之一:
*注意:需要添加以下导入:
引用
你可以在NoSuchElementException中找到一些相关的讨论: