我目前正在使用最新版本的 Selenium,并且如果我没有使用 quit 方法,Selenium 似乎会意外关闭。这是我的代码:
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]
要点击文章计数,您需要为element_to_be_clickable()引入WebDriverWait,并且您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
driver.get(url='https://en.wikipedia.org/wiki/Main_Page')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='articlecount']//a"))).click()
使用XPATH:
driver.get(url='https://en.wikipedia.org/wiki/Main_Page')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#articlecount a"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些关于 NoSuchElementException 的相关讨论: