英文:
Not able to click on checkbox using selenium in Python,Error- selenium.common.exceptions.ElementClickInterceptedException
问题
以下是您要翻译的内容:
错误信息如下:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input id="privacy_policy" type="checkbox" name="user[privacy_policy]" class="" value="0"> is not clickable at point (674, 592). Other element would receive the click: <p class="cookieinfo-text">...</p>
(Session info: chrome=114.0.5735.91)
from selenium import webdriver
from selenium.webdriver import chrome
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.support.wait import WebDriverWait
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
chromedriver_path = r'C:\Users\LENOVO\Downloads\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(service=Service(chromedriver_path))
driver.get('https://register.whirlpool.com/en-us/registration')
driver.maximize_window()
time.sleep(2)
# 查找并填写表单字段
model_field = driver.find_element('id', 'model')
time.sleep(2)
wait = WebDriverWait(driver, 20)
# checkbox = wait.until(EC.element_to_be_clickable(("id", "privacy_policy")))
# checkbox = wait.until(EC.element_to_be_clickable(("XPATH", "//*[@id='privacy_policy']")))
time.sleep(3)
checkbox = driver.find_element("id", "privacy_policy")
time.sleep(3)
checkbox.click()
var1 = driver.find_element("id", "privacy_policy").is_selected()
print(var1)
time.sleep(3)
希望这对您有所帮助。
英文:
I am trying to click on the checkbox on a website, getting below error, please suggest.
Error I am getting is:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input id="privacy_policy" type="checkbox" name="user[privacy_policy]" class="" value="0"> is not clickable at point (674, 592). Other element would receive the click: <p class="cookieinfo-text">...</p>
(Session info: chrome=114.0.5735.91)
from selenium import webdriver
from selenium.webdriver import chrome
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.support.wait import WebDriverWait
options =webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
chromedriver_path = r'C:\Users\LENOVO\Downloads\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(service=Service(chromedriver_path))
driver.get('https://register.whirlpool.com/en-us/registration')
driver.maximize_window()
time.sleep(2)
# Find and fill the form fields
model_field = driver.find_element('id', 'model')
time.sleep(2)
wait = WebDriverWait(driver, 20)
# checkbox = wait.until(EC.element_to_be_clickable(("id", "privacy_policy")))
# checkbox = wait.until(EC.element_to_be_clickable(("XPATH", "//*[@id='privacy_policy']")))
time.sleep(3)
checkbox = driver.find_element("id","privacy_policy")
time.sleep(3)
checkbox.click()
var1= driver.find_element("id","privacy_policy").is_selected()
print(var1)
time.sleep(3)
答案1
得分: 1
以下是点击“复选框”的解决方案:
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
driver = Chrome()
driver.get('https://register.whirlpool.com/en-us/registration')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'privacy_policy')))
driver.execute_script("document.getElementById('privacy_policy').click();")
time.sleep(2)
var1 = driver.find_element(By.ID, "privacy_policy").is_selected()
print(var1)
输出:
True
您还可以通过在页面的“控制台”上简单运行JavaScript查询 document.getElementById('privacy_policy').click()
来进行交叉检查,您将看到它确实点击了所需的“复选框”。
希望这解决了您的问题。
英文:
Here is the solution to click on the checkbox
:
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
driver = Chrome()
driver.get('https://register.whirlpool.com/en-us/registration')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'privacy_policy')))
driver.execute_script("document.getElementById('privacy_policy').click();")
time.sleep(2)
var1 = driver.find_element(By.ID, "privacy_policy").is_selected()
print(var1)
output:
True
You can also cross-check by simply running the javascript query document.getElementById('privacy_policy').click()
on the Console
of the page and you'll see that it indeed performs the click on the desired checkbox
.
I hope this solves your problem.
答案2
得分: 1
SOLUTION: 只需点击另一个元素,即包裹<input>
的<label>
元素。
所以,请将您的定位器从以下内容更改为:
checkbox = driver.find_element("id","privacy_policy")
更改为:
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='privacy_policy']"))).click()
完整的工作代码如下:
driver = webdriver.Chrome()
driver.get('https://register.whirlpool.com/en-us/registration')
driver.maximize_window()
wait = WebDriverWait(driver, 20)
# 下面一行点击“Okay, Got It”按钮
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Okay, Got It']"))).click()
# 下面一行点击复选框的<label>元素
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='privacy_policy']"))).click()
var1 = driver.find_element("id", "privacy_policy").is_selected()
print(var1)
time.sleep(10)
控制台输出:
True
结果:
注意:尽量避免使用JS执行操作。应该将其作为最后的手段。
英文:
If you notice the exception, selenium is not able to click <input>
element, and it clearly says the reason, other element will receive the click.
Message: element click intercepted: Element <input id="privacy_policy" is not clickable at point (759, 688). Other element would receive the click: <label for="privacy_policy">...</label>
(Session info: chrome=113.0.5672.129)
SOLUTION: Just click on that other element, which is the <label>
wrapping the <input>
.
So change your locator from:
checkbox = driver.find_element("id","privacy_policy")
To:
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='privacy_policy']"))).click()
Full Working code:
driver = webdriver.Chrome()
driver.get('https://register.whirlpool.com/en-us/registration')
driver.maximize_window()
wait = WebDriverWait(driver, 20)
# below line click on the Accept cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Okay, Got It']"))).click()
# below line clicks on the <label> element of the checkbox
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='privacy_policy']"))).click()
var1 = driver.find_element("id", "privacy_policy").is_selected()
print(var1)
time.sleep(10)
Console:
True
Result:
NOTE: Avoid using JS to perform actions as much as you can. It should be used as a last resort.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论