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

问题描述 投票:0回答:4

我正在使用下面的代码来单击您在随附的屏幕截图中看到的带有相应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

python selenium xpath css-selectors webdriverwait
4个回答
0
投票

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

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

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

这通常对我有用,在这种情况下,我会收到像您一样的“元素在某个时刻不可点击”错误。


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()

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()

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()
    

参考

您可以在以下位置找到几个相关的讨论:

© www.soinside.com 2019 - 2024. All rights reserved.