如何在使用selenium进行刮削时单击另一节中的同一按钮

2024-09-30 14:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在使用selenium进行抓取,我想单击“防御”部分中的“下一步”按钮,但我编写的代码在“摘要”中单击了“下一步”。你知道吗

以下是供您尝试的url:

https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/7361/Stages/16368/PlayerStatistics/England-Premier-League-2018-2019

所以它选择了“防御”,我可以在窗口中看到它被选中,但是下一页没有出现。单击“摘要”我发现下一个函数实际上正在那里发生。你知道吗

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

browser= webdriver.Chrome(executable_path ="C:\Program Files (x86)\Google\Chrome\chromedriver.exe") 
browser.get('https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/7361/Stages/16368/PlayerStatistics/England-Premier-League-2018-2019')


browser.find_element_by_xpath("""//*[@id="stage-top-player-stats-options"]/li[2]/a""").click()

element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, """//*[@id="next"]""")))
browser.execute_script("arguments[0].click();", element)

Tags: fromhttpsimportbrowsercomwwwseleniumelement
3条回答
  1. 元素定位器必须是唯一的
  2. 避免使用XPath通配符-*,因为它会导致性能下降和元素查找时间延长
  3. 避免使用JavaScriptExecutor进行点击,行为良好的Selenium测试必须像真实用户那样进行,我怀疑真实用户是否会打开浏览器控制台并键入document.getElementById('next').click()之类的内容,他是否会使用鼠标

假设以上所有内容,您应该在Defensive选项卡上找到一个唯一标识next按钮的选择器,它类似于:

//div[@id='statistics-paging-defensive']/descendant::a[@id='next']

参考文献:

对于每个选项卡(Summary,Defensive,…),添加相同的id=next到DOM的新下一步按钮。
选择防御性,你会看到下一个按钮有两个相同的id=next,选择进攻性,下一个按钮有三个。
使用基本的id=next选择器,您总是单击Summary选项卡中的第一个next按钮。因为您使用的是JavaScript,什么也没有发生,所以尝试使用Selenium click方法单击,您将得到一个错误。
要解决此问题,请调整选择器,使其更特定于dom-#statistics-paging-defensive #next。你知道吗

另外,当你第一次打开页面时,会出现cookies接受屏幕并阻止页面,你可以使用下面的方法跳过它。你知道吗

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


def accept_cookies():
    try:
        WebDriverWait(browser, 20)\
            .until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.qc-cmp-button")))\
            .click()
    except EX.NoSuchElementException or EX.TimeoutException:
        pass

#...

browser = webdriver.Chrome(executable_path ="C:\Program Files (x86)\Google\Chrome\chromedriver.exe") 
browser.get('https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/7361/Stages/16368/PlayerStatistics/England-Premier-League-2018-2019')

wait = WebDriverWait(browser, 20)
browser.get(baseUrl)

accept_cookies()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[href='#stage-top-player-stats-defensive']"))).click()
next_button = wait.until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "#statistics-paging-defensive #next")))
next_button.click()

xpath for next按钮对此页不是唯一的。试试这个

element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='stage-top-player-stats-defensive']//a[@id='next']")))
browser.execute_script("arguments[0].click();", element)

或者

element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='stage-top-player-stats-defensive']//a[@id='next']")))
element.click()

相关问题 更多 >