Selenium python如何执行单击切换按钮

Selenium python如何执行单击切换按钮,python,selenium,selenium-webdriver,web,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Web,Selenium Chromedriver,我正在尝试使用Python上的WebDriver在Selenium中单击调用循环切换 html代码: 这是按钮: 我尝试了几种选择: button_element = driver.find_element_by_class_name("iPhoneCheckContainer") button_element.click() 已存档的消息: Traceback (most recent call last): File "C:\Users\kosti\P

我正在尝试使用Python上的WebDriver在Selenium中单击调用循环切换

html代码:

这是按钮:

我尝试了几种选择:

button_element = driver.find_element_by_class_name("iPhoneCheckContainer")
button_element.click()
已存档的消息:

Traceback (most recent call last):
  File "C:\Users\kosti\PycharmProjects\test1\main.py", line 50, in <module>
    element = driver.find_element_by_xpath("//input[@id='on_off_on']").click()
  File "C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="checkbox" id="on_off_on" value="1" name="AllowReset" checked=""> is not clickable at point (171, 261). Other element would receive the click: <div class="iPhoneCheckHandle" style="width: 30px; left: 30px;">...</div>
  (Session info: chrome=88.0.4324.150)
回溯(最近一次呼叫最后一次):
文件“C:\Users\kosti\PycharmProjects\test1\main.py”,第50行,在
element=driver。通过xpath(//input[@id='on\u off\u on'])查找元素。单击()
文件“C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site packages\selenium\webdriver\remote\webelement.py”,第80行,单击
self.\u执行(命令。单击\u元素)
文件“C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site packages\selenium\webdriver\remote\webelement.py”,第633行,在\ u execute
返回self.\u parent.execute(命令,参数)
文件“C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site packages\selenium\webdriver\remote\webdriver.py”,执行中第321行
self.error\u handler.check\u响应(响应)
文件“C:\Users\kosti\AppData\Local\Programs\Python\Python39\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.element ClickInterceptedException:消息:元素click intercepted:元素在点(171261)处不可单击。其他元素将收到单击:。。。
(会话信息:chrome=88.0.4324.150)
尝试将输入id作为目标

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

elem=WebDriverWait(driver,5000).until(EC.visibility_of_element_located(
    (By.XPATH, "//input[@id='on_off_on']")))

driver.execute_script("arguments[0].scrollIntoView();",elem)

element.click()

尝试使用webdriver wait

请将html添加为代码只是他针对的是div而不是输入。请将html添加为代码而不是image@ArundeepChohan有时div也是可交互的示例,但这里可能是因为您提到的问题,但除非我们知道html是如何呈现的,否则很难说它不起作用。其他元素将收到单击:。。。(会话信息:chrome=88.0.4324.150)试试这个方法,让xpath转到f12,然后进入路径右键单击元素并复制复制xpath
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

elem=WebDriverWait(driver,5000).until(EC.visibility_of_element_located(
    (By.XPATH, "//input[@id='on_off_on']")))

driver.execute_script("arguments[0].scrollIntoView();",elem)

element.click()