【问题标题】:Selenium element is not clickable at point issue- PythonSelenium 元素在点问题时不可点击 - Python
【发布时间】:2020-10-16 18:41:49
【问题描述】:

我正在使用 selenium 在 python 中编写一个基本的自动化测试。我可以浏览多个页面,但是当我到达这一特定页面时,我无法点击按钮。

我的测试失败的代码

driver.find_element_by_id('//*[@id="save"]').click()

当我检查我试图点击的按钮时的元素

<input type="submit" value="View Report" id="save" name="save" data-reportid="108">

下面的错误信息

selenium.common.exceptions.ElementClickInterceptedException:消息: 元素点击被拦截:元素不可点击 点 (1750, 770)。其他元素会收到点击:

... (会话信息:chrome=83.0.4103.116)

【问题讨论】:

  • 应该是.find_element_by_xpath('//*[@id="save"]') 而不是.find_element_by_id('//*[@id="save"]')
  • 即使更新到 xpath 仍然是个问题

标签: selenium selenium-webdriver


【解决方案1】:

我遇到了同样的问题。 Selenium 没有单击我需要它单击的文本字段并引发相同的ElementClickInterceptedException

对我有用的是改变方法:

元素在那里。它已经加载了。我通过使用(在我的示例中)来确保它:

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "code")))

好的,那么问题是 Selenium 无法点击它。我们可以使用不同的策略:使用 JS!

使用上面的.until() 使元素可点击后,请记住将字段存储在变量中,或者使用上面的方法:

button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='save' and @name='save'][@value='View Report']")))` 

(不调用.click()

或通过找到它:

button = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']")

然后是黄金部分:通过JS点击:

driver.execute_script('arguments[0].click()', button)

为我工作,看看它是否对你有帮助

【讨论】:

    【解决方案2】:

    首先,获取您的窗口大小:

    driver.get_window_size()
    

    如果您的窗口大小小于您的可点击区域(可点击点 (width=1750, height=770)),您需要调整 selenium 驱动程序的窗口大小;

    driver.set_window_size(1750,800)
    

    【讨论】:

      【解决方案3】:

      当硒滚动到它时,元素可能被页眉或页脚覆盖。在点击向上滚动页面之前尝试。

       JavaScriptExecutor.ExecuteScript("window.scrollTo(0, 0);");
      

      【讨论】:

      • 在我使用 xpath 查找位置之后会出现这种情况吗?
      • 在定位元素之前滚动
      【解决方案4】:

      您需要在这里考虑几件事。根据元素的 HTML:

      <input type="submit" value="View Report" id="save" name="save" data-reportid="108">
      

      WebElement 是一个 React 元素。所以点击你需要诱导WebDriverWaitelement_to_be_clickable()的元素,你可以使用以下Locator Strategies之一:

      • 使用CSS_SELECTOR

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#save[name='save'][value='View Report']"))).click()
        
      • 使用XPATH

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='save' and @name='save'][@value='View Report']"))).click()
        
      • 注意:您必须添加以下导入:

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

      参考文献

      您可以在以下位置找到一些相关的讨论:

      【讨论】:

      • 在尝试 xpath 选项 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((by.xpath, "//input[@id='save' and @name ='save'][@value='View Report']"))).click() AttributeError: 'bytes' 对象没有属性 'element_to_be_clickable'
      • @PythonCoder4_09 不是by,而是By
      • 所以我将代码更新为WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.xpath, "//input[@id='save' and @name='save'][@value='View Report']"))).click() 仍然得到相同的错误 AttributeError: 'bytes' object has no attribute 'element_to_be_clickable' @DebanjanB
      • 对不起,我忘了导入其中一个包。现在已更正,但当我运行 xpath 或 css 选择器selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (482, 946) (Session info: chrome=83.0.4103.116)@DebanjanB 时我仍然遇到这个问题
      • @PythonCoder4_09 你能用完整的错误堆栈跟踪更新问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 2020-02-23
      • 2015-06-13
      相关资源
      最近更新 更多