Selenium 复选框点击

huangapple go评论81阅读模式
英文:

Selenium checkbox click

问题

I am practicing writing code using Selenium. I am having trouble trying to make the code mark the terms and conditions check box, but it does not seem to work. The website I am practicing on is https://www.starbucks.com/account/create

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = driver.find_element(By.ID, "termsAndConditions").click()

Another code I tried was:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.starbucks.com/account/create")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[3]/main/div[2]/div/div/form/div/div[1]/div[1]/label/span/span[1]/span/svg'))).click()

Neither of these codes seems to work to mark the checkbox.

英文:

I am practicing writing code using Selenium I am having trouble trying to make the code mark the terms and conditions check box but it does not seem to work. the website the website I am practicing on is https://www.starbucks.com/account/create

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = driver.find_element(By.ID, "termsAndConditions").click()

another code i tried was

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("https://www.starbucks.com/account/create")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[3]/main/div[2]/div/div/form/div/div[1]/div[1]/label/span/span[1]/span/svg'))).click()

neither seem to work to mark the checkbox

答案1

得分: 0

你必须在执行以下代码之前关闭cookies弹窗:

driver.get("https://www.starbucks.com/account/create")
driver.implicitly_wait(2)
time.sleep(3)
# 接受cookies
elem = driver.find_element(By.ID, "truste-consent-button").click()
time.sleep(1)
driver.find_element(By.XPATH, '//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]//span[@class="block option__labelMarker"]').click()
time.sleep(3000)
英文:

You have to close cookies popup before:

driver.get("https://www.starbucks.com/account/create")
driver.implicitly_wait(2)
time.sleep(3)
# Accept cookies
elem = driver.find_element(By.ID, "truste-consent-button").click()
time.sleep(1)
driver.find_element(By.XPATH, '//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]//span[@class="block option__labelMarker"]').click()
time.sleep(3000)

答案2

得分: 0

The terms and conditions checkbox is blocked by another element, so calling click() does not work. You could instead use JavaScript to click it (or directly set its checked property).

driver.execute_script('arguments[0].click()', driver.find_element(By.ID, 'termsAndConditions'))
英文:

The terms and conditions checkbox is blocked by another element, so calling click() does not work. You could instead use JavaScript to click it (or directly set its checked property).

driver.execute_script('arguments[0].click()', driver.find_element(By.ID, 'termsAndConditions'))

答案3

得分: 0

以下是使用XPath的方法:

driver.find_element(By.XPATH, '//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]//span[@class="block option__labelMarker"]').click()

处理说明

在浏览页面时,我尝试选择并点击以下元素:
'//label[@class="option option--checkbox"]/span'
但是,它点击了“是的,我想要电子邮件”复选框。

这意味着我们需要更明确地指定我们需要哪个复选框:
因此,'//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]' 部分正是如此:找到位于“Terms of Use”标题之后的复选框。

然后,我只是尝试点击不同的子元素,其中一个正好起作用。

不要依赖于这些浏览器自动生成的XPath,因为它们在网站中的某些更改时往往容易出错。请尽量使用类名或元素内容。

英文:

Here is a way to make it using xpath

driver.find_element(By.XPATH, '//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]//span[@class="block option__labelMarker"]').click()

Process explanation:

When exploring the page, I tried to select and click the following element:
'//label[@class="option option--checkbox"]/span'
however, it clicks on the "Yes, I want emails" checkbox.

This means we need to be more specific on which checkbox do we need:
so '//div[h2[contains(text(), "Terms of Use")]]//label[@class="option option--checkbox"]' part does exactly that: find the checkbox which is located after the "Terms of Use" header

Then I just tried to click on the different sub-elements and one of them just worked.

Do not rely on these browser auto-generated xpaths as they tend to break often when something is changed in the website. Try to stick with class names or element contents instead.

huangapple
  • 本文由 发表于 2023年5月17日 09:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定