我正在构建一个网络刮板,该刮板可从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)
我知道这里还有其他类似的问题,但是没有一个能够帮助我。谢谢!
答案 0 :(得分:0)
使用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()
在使用代码之前,请确保已导入以上导入内容。
答案 1 :(得分:0)
这里确实有两个问题。尝试使用click()
时,您需要为element_to_be_clickable()
引入 WebDriverWait ,并且可以使用以下任一Locator Strategies:
使用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 之间存在明显的不匹配,要解决该问题,您需要执行以下任一操作: