单击带有 Selenium 按钮的按钮
Clicking a button with Selenium button
我正在尝试点击此网站上的“同意”按钮 https://www.soccerstats.com/matches.asp?matchday=1#,但使用此代码对我不起作用:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
driver.find_element(By.CLASS_NAME," css-47sehv").click()
css-47sehv
是按钮的 class 名称,这是按钮的图片 The blue button
试试这个
driver.find_element_by_class_name('css-47sehv').click()
代替
driver.find_element(By.CLASS_NAME," css-47sehv").click()
要单击 AGREE
按钮,请使用以下 xpath
来识别元素并单击。
//button[text()='AGREE']
代码:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
或使用以下 css 选择器。
driver.find_element(By.CSS_SELECTOR,"button.css-47sehv").click()
您必须确保以下几点:
1-显式使用Wait等待按钮to/Till出现
try:
element=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID, "AgreeButton"))
)
finally:
driver.quit()
2-单击具有正确 Xpath 的按钮:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
3-如果简单的点击不起作用,您可以使用 JavaScript 并执行点击方法。
尽管元素 AGREE 包含类名 css-47sehv
,但该值看起来是动态的,可能会在短时间内发生变化或一旦应用程序重新启动。
解决方案
点击需要诱导的元素WebDriverWait for the and you can use either of the following :
使用CSS_SELECTOR:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
使用 XPATH:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary' and text()='AGREE']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在尝试点击此网站上的“同意”按钮 https://www.soccerstats.com/matches.asp?matchday=1#,但使用此代码对我不起作用:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
driver.find_element(By.CLASS_NAME," css-47sehv").click()
css-47sehv
是按钮的 class 名称,这是按钮的图片 The blue button
试试这个
driver.find_element_by_class_name('css-47sehv').click()
代替
driver.find_element(By.CLASS_NAME," css-47sehv").click()
要单击 AGREE
按钮,请使用以下 xpath
来识别元素并单击。
//button[text()='AGREE']
代码:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
或使用以下 css 选择器。
driver.find_element(By.CSS_SELECTOR,"button.css-47sehv").click()
您必须确保以下几点:
1-显式使用Wait等待按钮to/Till出现
try:
element=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID, "AgreeButton"))
)
finally:
driver.quit()
2-单击具有正确 Xpath 的按钮:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
3-如果简单的点击不起作用,您可以使用 JavaScript 并执行点击方法。
尽管元素 AGREE 包含类名 css-47sehv
,但该值看起来是动态的,可能会在短时间内发生变化或一旦应用程序重新启动。
解决方案
点击需要诱导的元素WebDriverWait for the
使用CSS_SELECTOR:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
使用 XPATH:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary' and text()='AGREE']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC