Python Selenium:元素在点处不可单击

时间:2019-11-25 23:39:23

标签: python selenium xpath css-selectors webdriverwait

我正在使用以下代码单击随附的屏幕截图中带有相应HTML代码的突出显示的选项卡。该选项卡始终在脚本底部可见,因此无需向下滚动至该选项卡。

element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))
element.click()

我得到的错误如下。我在做什么错了?

Message: unknown error: Element <a class="tab-button has-icon icon-only" href="#" role="tab" ng- 
reflect-tab="[object Object]" id="tab-button-settings" aria-controls="tabpanel-t0-1" aria- 
selected="false">...</a> is not clickable at point (1440, 1017). Other element would receive the 
click: <div class="click-block click-block-enabled click-block-active"></div>
(Session info: chrome=78.0.3904.108)

enter image description here

4 个答案:

答案 0 :(得分:0)

您可以尝试通过使用JavaScript单击来解决此问题:

element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))

driver.execute_script("arguments[0].click();", element)

在我收到像您一样的“元素无法点击时”错误的情况下,这通常对我有用。

答案 1 :(得分:0)

JavaScript的另一种替代方法是使用触摸操作

from selenium.webdriver.common.touch_actions import TouchActions

element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))

touchactions = TouchActions(driver)
touchactions.tap(element).perform()

答案 2 :(得分:0)

您可以使用操作类来避免上述问题

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

from selenium.webdriver.common.action_chains import ActionChains

element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings"))
ActionChains(driver).move_to_element(element).click().perform()

答案 3 :(得分:0)

所需元素是Angular元素,因此必须在元素上定位click()并为element_to_be_clickable()引入 WebDriverWait ,您可以使用其中一个以下Locator Strategies中的

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.tab-button.has-icon.icon-only#tab-button-settings"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='tab-button has-icon icon-only' and @id='tab-button-settings']"))).click()
    
  • 注意:您必须添加以下导入:

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

如果unknown error仍然发生,则可能需要为invisibility_of_element_located()额外诱导 WebDriverWait ,并且可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.click-block.click-block-enabled.click-block-active")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.tab-button.has-icon.icon-only#tab-button-settings"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='click-block click-block-enabled click-block-active']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='tab-button has-icon icon-only' and @id='tab-button-settings']"))).click()
    

参考

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