简体   繁体   中英

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.

在此处输入图片说明

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

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.

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

You can remove the div by running some JavaScript code as follows:

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.

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

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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