[英]How to click on a button using Selenium
I'm making some test with selenium on this page: https://www.justwatch.com/es/proveedor/hbo-max/peliculas我正在此页面上使用 selenium 进行一些测试: https://www.justwatch.com/es/proveedor/hbo-max/peliculas
The first time you get in the page you must accept the privacy settings but i cannot get it to work.第一次进入该页面时,您必须接受隐私设置,但我无法使其正常工作。 I've tried all the methods i now but it seems like i cant find the button i want so the program stops as the waiting time ends.我已经尝试了我现在的所有方法,但似乎我找不到我想要的按钮,所以程序在等待时间结束时停止。 The button i whant to click is:我要点击的按钮是:
<button role="button" data-testid="uc-save-button" class="sc-gsDKAQ eaUldE" style="margin: 0px 6px;">Guardar configuración</button>
I tried to locate the button by text, class and using the XPath but doesnt work.我试图通过文本 class 并使用 XPath 来定位按钮,但不起作用。
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sc-gsDKAQ eaUldE'.replace(' ', '.'))))\
.click()
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]//div/div/div/div/div[2]/div/div[2]/div/div/div/button[1]')))\
.click()
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Guardar configuración')))\
.click()
I will aprecciate your help.我会感谢你的帮助。
To click on the desired element you can use either of the following locator strategies :要单击所需的元素,您可以使用以下任一定位器策略:
Using CSS_SELECTOR :使用CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='uc-save-button']"))).click()
Using XPATH :使用XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='uc-save-button' and text()='Guardar configuración']"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.