Not able to click on checkbox using selenium in Python,Error- selenium.common.exceptions.ElementClickInterceptedException

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

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

结果:

Not able to click on checkbox using selenium in Python,Error- selenium.common.exceptions.ElementClickInterceptedException

注意:尽量避免使用JS执行操作。应该将其作为最后的手段。

英文:

If you notice the exception, selenium is not able to click &lt;input&gt; element, and it clearly says the reason, other element will receive the click.

Message: element click intercepted: Element &lt;input id=&quot;privacy_policy&quot; is not clickable at point (759, 688). Other element would receive the click: &lt;label for=&quot;privacy_policy&quot;&gt;...&lt;/label&gt;
  (Session info: chrome=113.0.5672.129)

SOLUTION: Just click on that other element, which is the &lt;label&gt; wrapping the &lt;input&gt;.

So change your locator from:

checkbox = driver.find_element(&quot;id&quot;,&quot;privacy_policy&quot;)

To:

wait.until(EC.element_to_be_clickable((By.XPATH, &quot;//label[@for=&#39;privacy_policy&#39;]&quot;))).click()

Full Working code:

driver = webdriver.Chrome()
driver.get(&#39;https://register.whirlpool.com/en-us/registration&#39;)
driver.maximize_window()

wait = WebDriverWait(driver, 20)
# below line click on the Accept cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, &quot;//button[text()=&#39;Okay, Got It&#39;]&quot;))).click()
# below line clicks on the &lt;label&gt; element of the checkbox
wait.until(EC.element_to_be_clickable((By.XPATH, &quot;//label[@for=&#39;privacy_policy&#39;]&quot;))).click()

var1 = driver.find_element(&quot;id&quot;, &quot;privacy_policy&quot;).is_selected()
print(var1)
time.sleep(10)

Console:

True

Result:

Not able to click on checkbox using selenium in Python,Error- selenium.common.exceptions.ElementClickInterceptedException

NOTE: Avoid using JS to perform actions as much as you can. It should be used as a last resort.

huangapple
  • 本文由 发表于 2023年6月5日 15:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404208.html
匿名

发表评论

匿名网友

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

确定