Python Selenium 无法点击元素,因为其他元素遮住了它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49921128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:18:31  来源:igfitidea点击:

Selenium can't click element because other element obscures it

pythonseleniumelement

提问by LucSpan

Set-up

设置

I'm using Python 3.x and Selenium to fill out a query field and subsequently click the search button,

我使用 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()

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

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



Problem

问题

Executing the code above gives the following error,

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

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

I can solve this error by inserting a hard wait before grabbing and clicking the button, like so,

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

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

But I rather solve it differently, so I followed this answerand did the following,

但我宁愿以不同的方式解决它,所以我遵循了这个答案并执行了以下操作,

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

But I got exactly the same error.

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

I also tried this answer, but same error.

我也试过这个答案,但同样的错误。

How do I solve this?

我该如何解决这个问题?

采纳答案by LucSpan

Following nr.5 of DebanjanB's answer, I solved it by implying the code to wait for the temporary overlay to dissapear before trying to click,

遵循 debanjanB 的nr.5 答案,我通过暗示代码在尝试单击之前等待临时覆盖消失来解决它,

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

回答by Anand

There are several ways to do this, one of the ways is by Javascript executor.

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

You could say:

你可以说:

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

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

This way, you can block the div with class = 'blockUI blockOverlay'and your element can be clicked if I'm correct.

这样,您可以使用 div 来阻止 div,class = 'blockUI blockOverlay'如果我是正确的,则可以单击您的元素。