在Web of Science上查找并单击隐藏的下拉列表



您可以通过在私人浏览模式下打开链接或直接运行我的代码来避免注册科学网帐户。

我需要从科学网获取大规模数据。你可以点击这里查看。我正在尝试使用python selenium来解决这个问题。这是我的代码

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
link: str = "https://www.webofscience.com/wos/woscc/summary/9110e4d1-3d85-49d2-81b2-af843061855d-55cb3b26/relevance/1"
def initial():
driver: webdriver = webdriver.Chrome(service=Service(driverPath))
driver.get(link) # open website by chorme
cookieschose = driver.find_element("id", "onetrust-accept-btn-handler")  # locate cookies setting button
actchian = ActionChains(driver)
actchian.move_to_element(to_element=cookieschose).click().perform()  # where the bug appeared
time.sleep(2)    
export = driver.find_element("class name", "mat-button-wrapper")  # find export button
actchian.click(on_element=export).perform()  # click export button
# excel = driver.find_element("xpath", """//*[@id="exportToExcelButton"]""") # where the same bug will appear
# actchian.click(excel).perform()

棘手的是我在第一步就被卡住了。一旦你进入网页,它需要你的cookie的许可,你需要点击一个按钮。我设法定位它并将它存储在变量"0"中;库克选择了";。但我的电脑会显示";selenium.com.mon.exceptions.MoveTargetOutOfBoundsException:消息:将目标移动到界外">
当它执行";actchian.move_to_element(to_element=cookieselected(.click((.perform((">

在我修改";move_to_element"至";单击"更令人沮丧的是,后来我需要订购电脑来点击";"出口";按钮并选择";excel";从选项列表中选择。但现在我甚至无法确定我的代码是否正确运行。

总而言之,我有两个问题:

  1. 如何修复错误
  2. 我如何才能知道我的python selenium自动代码是否以正确的方式运行
  3. 如果你愿意,请帮我查找并点击";excel";在导出选项列表中。它将只在您点击";"出口";。通常,它是隐藏的。你可以在下面看到它的结构,或者点击上面的链接
    如果你能帮我的话,提前谢谢你

导出->excel可供用户和访问者使用。

以下是cookie页面的HTML代码部分:

<button id="onetrust-accept-btn-handler">accept all Cookie</button>

代码的"部分;excel";在选项列表中:

<button mat-menu-item="" 
class="mat-focus-indicator export-option mat-menu-item ng-star-inserted"  id="exportToExcelButton" 
aria-label="Excel" role="menuitem" tabindex="0" 
aria-disabled="false"> Excel <div matripple="" class="mat-ripple mat-menu-ripple"></div><!----></button>

尝试使用WebDriverWait((并等待元素可点击。

wait=WebDriverWait(driver, 20)
#click on cookie
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
#click on export button
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#exportToExcelButton"))).click()

导入以下库。

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

如果上述解决方案给您元素的可交互性问题。尝试使用js脚本点击作为解决方法。

wait=WebDriverWait(driver, 20)
#click on cookie
cookietbtn=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler")))
driver.execute_script("arguments[0].click();", cookietbtn)
#click on export button
exportbutton=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "button#exportToExcelButton")))
driver.execute_script("arguments[0].click();", exportbutton)

最新更新