Javascript 找到Python Selenium Webdriver元素,但无法单击它
目标是单击登录页面上的复选框。 我通过XPATH找到元素,但无法单击它Javascript 找到Python Selenium Webdriver元素,但无法单击它,javascript,python,selenium,checkbox,webdriver,Javascript,Python,Selenium,Checkbox,Webdriver,目标是单击登录页面上的复选框。 我通过XPATH找到元素,但无法单击它 >>> elem = driver.find_element_by_xpath("//input[@type='checkbox'][@name='conditions']") >>> elem.is_displayed() False >>> elem.is_enabled() True >>> elem.get_attribute('outerHTM
>>> elem = driver.find_element_by_xpath("//input[@type='checkbox'][@name='conditions']")
>>> elem.is_displayed()
False
>>> elem.is_enabled()
True
>>> elem.get_attribute('outerHTML')
u'<input type="checkbox" class="custom-control-input" name="conditions">'
尝试elem.click时,出现异常:
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见,但元素清晰可见,因为页面已加载,并且我从终端工作
使用不同选择器时的其他错误是:
驱动程序。通过xpath“/html/body/div[2]/main/section/div/div[3]/div/div[1]/form/p[1]/label/input”查找元素。单击
selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点51549处不可单击。其他元素将收到单击:
我尝试了注入JavaScript,但没有成功。
driver.execute_scriptarguments[0]。style.visibility='visible';,元素
有没有办法解决这个问题 您可以使用以下XPATH:-//输入[@type='checkbox'和@name='conditions']
对于以下错误:-使用“单击前等待”事件
selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点51549处不可单击。当您尝试以下操作时,其他元素将根据您的代码试用收到单击:…:
elem.click()
elem.is_enabled()
你看到:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
False
True
这意味着所需的元素仍然不可见
即使在尝试单击之前,当您正在尝试时:
elem.is_displayed()
你看到:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
False
True
但当你尝试时:
elem.click()
elem.is_enabled()
你看到:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
False
True
因此,结合所有这些观察结果,可以是以下任一情况:
元素存在于DOM中,但仍然不可见/不可交互。在这种情况下,您需要诱导WebDriverWait,然后按如下方式调用click:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='checkbox' and @name='conditions']"))).click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].scrollIntoView(true);", elem)
elem.click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.click()
元素存在于DOM中,但不在中。在这种情况下,您需要调用execute_脚本将元素放入视口中,然后调用click,如下所示:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='checkbox' and @name='conditions']"))).click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].scrollIntoView(true);", elem)
elem.click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.click()
您采用的定位策略可能不是唯一的,它标识了多个WebElement,并且要标识的第一个元素可能被隐藏。在这种情况下,is_display将始终返回False,并且您必须构造唯一的定位器策略,该策略唯一地标识所需的元素。
元素的style属性可能设置为display:none;在这种情况下,您必须按如下方式使用execute_脚本方法:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='checkbox' and @name='conditions']"))).click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].scrollIntoView(true);", elem)
elem.click()
elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.click()
尝试显式地等待元素的可见性,然后单击它。感谢@DebanjanB伟大的回答,我尝试并仔细检查了所有元素,但总是得到错误:元素。。。在点处不可单击。。。selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点51286处不可单击。其他元素将收到单击:。。。