简体   繁体   English

无法点击带有硒的按钮

[英]Cannot click on button with selenium

I have the following website where I want to click on the button "SKIP THIS AD" that popup after waiting x seconds.我有以下网站,我想单击等待 x 秒后弹出的按钮“跳过此广告”。

在此处输入图片说明

My code is as follows:我的代码如下:

 import selenium
 from selenium import webdriver

 driver = webdriver.Chrome()
 driver.get('http://festyy.com/wpixmC')
 sleep(10)

 driver.find_element_by_xpath('/html/body/div[3]/div[1]/span[5]').click()

However, when I inspect the element I don't see a connected link to be clicked.但是,当我检查元素时,我没有看到要单击的连接链接。 In addition, I get另外,我得到

ElementClickInterceptedException: Message: element click intercepted: Element <span class="skip-btn 
show" id="skip_button" style="cursor: pointer">...</span> is not clickable at point (765, 31). Other 
element would receive the click: <div style="position: absolute; top: 0px; left: 0px; width: 869px; 
height: 556px; z-index: 2147483647; pointer-events: auto;"></div>

Somehow it seems that everything is redirected to the larger class?不知何故,似乎一切都被重定向到更大的班级? How can I overcome this?我怎样才能克服这个问题? When I try to copy the xpath I get also only the following: /div当我尝试复制 xpath 时,我也只得到以下内容: /div

Thanks in advance提前致谢

It seems that the error that you receive ('element click intercepted') is due to the fact that there is a div that is placed on page load, that takes up the whole page, preventing Selenium from clicking on the skip button.您收到的错误('元素点击被拦截')似乎是由于页面加载时放置了一个 div,它占据了整个页面,阻止了 Selenium 单击跳过按钮。

Therefore you have to remove that div first and then run this: driver.find_element_by_xpath('/html/body/div[3]/div[1]/span[5]').click()因此,您必须先删除该 div,然后运行: driver.find_element_by_xpath('/html/body/div[3]/div[1]/span[5]').click()

You can remove the div by running some JavaScript code as follows:您可以通过运行一些 JavaScript 代码来删除 div,如下所示:

driver.execute_script("""
var badDivSelector = document.evaluate('/html/body/div[7]', 
document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, 
null);
if (badDivSelector) {
var badDiv = badDivSelector.singleNodeValue;
badDiv.parentNode.removeChild(badDiv);
}
""")

The code above finds the full page div (identified by xpath) and removes it from the page.上面的代码找到整个页面的 div(由 xpath 标识)并将其从页面中删除。

Your final code should look something like this:您的最终代码应如下所示:

import selenium
from selenium import webdriver

from time import sleep

driver = webdriver.Chrome()
driver.get('http://festyy.com/wpixmC')

sleep(10)

driver.execute_script("""
var badDivSelector = document.evaluate('/html/body/div[7]', 
document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, 
null)
if (badDivSelector) {
var badDiv = badDivSelector.singleNodeValue;
badDiv.parentNode.removeChild(badDiv);
}
""")

driver.find_element_by_xpath('/html/body/div[3]/div[1]/span[5]').click()

....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM