为什么复选框可以在控制台中单击,而在Selenium代码中却不能单击?

时间:2020-07-16 20:15:08

标签: python selenium xpath css-selectors webdriverwait

我有一个要单击的元素(复选框):

<ins tabindex="0" role="checkbox" class="check-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; cursor: pointer;" aria-checked="false" aria-disabled="false"></ins>

我可以使用Selenium Python查找元素...

element = driver.find_element_by_class_name('check-helper')

...但是单击它(element.click())会导致此跟踪:

---------------------------------------------------------------------------
ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-379-cf25f85114ab> in <module>
----> 1 element.click()

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=78.0.3904.97)

当我在控制台中找到该元素并使用$0.click()单击该元素时,将选中该复选框。我尝试在代码中使用JavaScript来点击它...

element = driver.find_element_by_class_name('check-helper')
driver.execute_script("arguments[0].click();", element)

...但是这导致它没有错误运行,但实际上没有单击该复选框。

控制台和我的代码之间的断开位置在哪里,为什么我不能从我的代码中单击此元素?

2 个答案:

答案 0 :(得分:0)

要单击元素,您必须为gatsby build && gatsby serve引入WebDriverWait,并且可以使用以下任一Locator Strategies

  • 使用// gatsby-config.js module.exports = { siteMetadata: { ... }, plugins: [ `gatsby-plugin-react-helmet`, { resolve: `gatsby-source-filesystem`, options: { name: `images`, path: `${__dirname}/src/images`, }, }, `gatsby-transformer-sharp`, `gatsby-plugin-sharp`, { resolve: `gatsby-plugin-manifest`, options: { name: "Peak Websites", short_name: "Peak Websites", start_url: "/", background_color: "#27688E", theme_color: "#27688E", display: "minimal-ui", icon: "src/images/logo/Logo_squared.png", }, }, { resolve: `gatsby-source-wordpress`, options: { baseUrl: `pathtomysite.com`, protocol: `https`, useACF: false, verboseOutput: false, hostingWPCOM: false } }, `gatsby-plugin-sass`, { resolve: `gatsby-plugin-prefetch-google-fonts`, options: { fonts: [{ family: `Montserrat`, variants: [`300`,`300i`,`400`,`400i`,`700`] }], }, }, { resolve: `gatsby-plugin-google-analytics`, options: { trackingId: process.env.GA_TRACKING_CODE, head: false, }, }, // `gatsby-plugin-sitemap` // this (optional) plugin enables Progressive Web App + Offline functionality // To learn more, visit: https://gatsby.dev/offline // `gatsby-plugin-offline`, ], }

    element_to_be_clickable()
  • 使用CLASS_NAME

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "check-helper"))).click()
    
  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ins.check-helper"))).click()
    
  • 注意:您必须添加以下导入:

    XPATH

答案 1 :(得分:0)

selenium.common.exceptions.ElementNotInteractableException文档:

exception 
selenium.common.exceptions.ElementNotInteractableException(msg=None, screen=None, stacktrace=None)
    Bases: selenium.common.exceptions.InvalidElementStateException

当DOM中存在某个元素但与该元素的交互将击中另一个元素时会引发绘制顺序

要解决此问题,请尝试使用selenium.webdriver.common.action_chains.ActionChains

element = driver.find_element_by_class_name('check-helper')
ActionChains(driver).move_to_element(element).click(element).perform()

正在导入:

from selenium.webdriver import ActionChains