![](/img/trans.png)
[英]selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error using Selenium through Python
[英]selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium ChromeDriver Chrome with Python
我正在構建一個Web抓取器,用於從Web of Science抓取引文數據。 在星期五,我完成了程序的那部分,但是今天它已經停止工作,當我嘗試通過Selenium訪問引文數據時,我收到了NoSuchElementException。 我嘗試使用不同的方式(id,class,xpath,css選擇器)單擊這一特定元素,但是它總是會引發錯誤。 這是我的代碼,正在運行,現在不起作用:
url = 'https://apps.webofknowledge.com/Search.do?product=UA&SID=8F2pCcE8ApJDSKZLHfF&search_mode=GeneralSearch&prID=acd62bc2-0ee0-47a1-a85d-12009db3c2f5'
driver.get(url)
citers_num = driver.find_element_by_class_name('snowplow-citation-network-times-cited-count-link')
citers_num.click()
這是html:
<div class="search-results-data-cite">Times Cited: <a class="snowplow- times-cited-link" title="View all of the articles that cite this one" href="/CitingArticles.do product=WOS&SID=5FAYgZP1cYhuG9LGN3I&search_mode=CitingArticles&parentProduct=WOS&parentQid=18&parentDoc=12&REFID=84460199&excludeEventConfig=ExcludeIfFromNonInterProduct">313</a>
這是我今天遇到的錯誤:
citers_num = driver.find_element_by_class_name('snowplow-citation-network-all-times-cited')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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":"class name","selector":"snowplow-citation-network-all-times-cited"}
(Session info: chrome=75.0.3770.80)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.1 x86_64)
我知道這里還有其他類似的問題,但是沒有一個能夠幫助我。 謝謝!
使用Webdriverwait
並按照xpath
進行單擊。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search-results-data-cite'][contains(.,'Times Cited:')]/a"))).click()
或在CSS選擇器后面跟隨CSS。
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.search-results-data-cite a"))).click()
在使用代碼之前,請確保已導入了高於導入的內容。
確實有兩個問題。 在嘗試click()
您需要為element_to_be_clickable()
誘導WebDriverWait ,並且可以使用以下兩種定位策略之一 :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.search-results-data-cite a[class*='times-cited-link'][href^='/CitingArticles']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='search-results-data-cite']//a[@title='View all of the articles that cite this one' and starts-with(@href, '/CitingArticles')]"))).click()
注意 :您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
下一個問題是正在使用的二進制版本之間的不兼容 ,如下所示:
支持Chrome v74
支持Chrome v75
因此, ChromeDriver v74.0與Chrome瀏覽器v75.0之間存在明顯的不匹配,要解決該問題,您需要執行以下任一操作:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.