【问题标题】:Selenium can't click element because other element obscures itSelenium 无法单击元素,因为其他元素会遮挡它
【发布时间】:2018-04-19 12:27:37
【问题描述】:

设置

我正在使用 Python 3.x 和 Selenium 填写查询字段,然后单击搜索按钮,

# element containing the product search bar and buttons
search_area = el_id('Products').find_element_by_class_name('searchArea')

# insert name of file to be duplicated
name_field = search_area.find_element_by_xpath("//input[@type='text']")
name_field.clear()
name_field.send_keys('to_be_duplicated')  

# click search button
search_area.find_element_by_xpath('span/a[1]').click()

el_id(x) = browser.find_element_by_id(x).


问题

执行上面的代码会出现以下错误,

ElementClickInterceptedException: Element <a class="button button-fleft searchButton" href="#"> is not clickable at point (577.6166763305664,225.06666564941406) because another element <div class="blockUI blockOverlay"> obscures it

我可以通过在抓取并单击按钮之前插入一个硬等待来解决此错误,就像这样,

# click search button
time.sleep(1)
search_area.find_element_by_xpath('span/a[1]').click()

但我宁愿用不同的方式解决它,所以我跟着this answer做了以下,

# click search button
search_button = search_area.find_element_by_xpath('span/a[1]')
WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.XPATH, 
"//*[@id="Products"]/tbody/tr[1]/td/div/input")))
search_button.click()

但我得到了完全相同的错误。

我也试过this answer,但同样的错误。

我该如何解决这个问题?

【问题讨论】:

标签: python selenium element


【解决方案1】:

按照DebanjanB's answer 的nr.5,我通过暗示代码在尝试点击之前等待临时叠加层消失来解决它,

wait.until(EC.invisibility_of_element_located((By.XPATH,
              "//div[@class='blockUI blockOverlay']")))
el_xp("//input[@value='Save']").click()

【讨论】:

  • 如何检测临时覆盖的 xpath?我试图在 Chrome 开发工具上检测到它,但由于它很快消失,我无法知道 xpath。
  • 我试过了。等待它消失。说完就说消失了,但是之后我尝试点击的时候还是报错。
  • 在我的例子中,叠加层是一个广告,它不是临时的,它永远不会消失。
【解决方案2】:

有几种方法可以做到这一点,其中一种方法是通过 Javascript 执行器。

你可以说:

element = driver.find_element_by_xpath("//div[@class='blockUI blockOverlay']")

driver.execute_script("arguments[0].style.visibility='hidden'", element)

这样,你就可以用class = 'blockUI blockOverlay'屏蔽这个div了 如果我是正确的,你的元素可以被点击。

【讨论】:

    【解决方案3】:

    另外,您可以尝试点击JavaScript 的元素,如下所示:

    
    # element containing the product search bar and buttons
    search_area = el_id('Products').find_element_by_class_name('searchArea')
    
    # click element by executing javascript
    driver.execute_script("arguments[0].click();", search_area)
    
    

    【讨论】:

    • 它在我的情况下工作,我认为因为在元素中调用 click() 会做一些事情,比如带有 (x,y) 位置的“虚拟点击”,但是调用 click() 使用 execute_script 会一些如何直接调用这个元素的脚本
    猜你喜欢
    • 2019-04-09
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2019-07-05
    相关资源
    最近更新 更多