Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用selenium单击按钮_Python_Selenium - Fatal编程技术网

Python 无法使用selenium单击按钮

Python 无法使用selenium单击按钮,python,selenium,Python,Selenium,我有下面的网站,我想点击按钮“跳过这个广告”,等待x秒后弹出 我的代码如下: 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() driv

我有下面的网站,我想点击按钮“跳过这个广告”,等待x秒后弹出

我的代码如下:

 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()
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);
}
""")
但是,当我检查元素时,我没有看到要单击的连接链接。此外,我得到

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>
element单击拦截异常:消息:element单击拦截:element。。。在点(765,31)处不可单击。其他
元素将收到单击:
不知何故,似乎一切都被重定向到了更大的类?我怎样才能克服这个问题?当我尝试复制xpath时,也只得到以下内容:
/div


提前感谢

您收到的错误(“元素点击被截获”)似乎是因为页面加载时有一个div占据了整个页面,阻止Selenium点击跳过按钮

因此,必须先删除该div,然后运行以下操作:
driver。通过xpath('/html/body/div[3]/div[1]/span[5]')查找元素。单击()

您可以通过运行一些JavaScript代码来删除div,如下所示:

 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()
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);
}
""")
上面的代码查找完整的页面div(由xpath标识)并将其从页面中删除

您的最终代码应该如下所示:

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

....

似乎是通过编程在该div上检测到了click事件。单击大约两次(在弹出广告后),重定向的JavaScript代码被激活。这意味着HTML本身没有硬链接。可能是因为屏幕上有警报吗?