簡體   English   中英

selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法使用帶有Python的Selenium ChromeDriver Chrome找到元素

[英]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&amp;SID=5FAYgZP1cYhuG9LGN3I&amp;search_mode=CitingArticles&amp;parentProduct=WOS&amp;parentQid=18&amp;parentDoc=12&amp;REFID=84460199&amp;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 

下一個問題是正在使用的二進制版本之間的不兼容 ,如下所示:

  • 您正在使用chromedriver = 74.0
  • chromedriver = 74.0發行說明明確提到以下內容:

支持Chrome v74

  • 您使用的chrome = 75.0已通過最近的推送進行了更新。
  • ChromeDriver v75.0的發行說明中明確提到以下內容:

支持Chrome v75

因此, ChromeDriver v74.0Chrome瀏覽器v75.0之間存在明顯的不匹配,要解決該問題,您需要執行以下任一操作:

  • Chrome V75和升級ChromeDriverChromeDriver V75水平。
  • 保留ChromeDriver v74並將Chrome降級為Chrome v74級別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM