Close pop up by pressing anywhere on the screen

Viewed 103

I'm trying to close the pop-up by trying to simulate a click on the body of the web page. It is possible to locate the close button and close it. However, I am trying to close the pop-up as there can be 1-3 pop-ups in some pages and all can be closed with a single click on the body. (easier to automate) Is there a way to simulate a body click to close that pop-up.

I have tried :

 driver.find_element_by_tag_name('body').click()
 driver.find_element_by_tag_name('div').click()
 driver.execute_script("document.getElementByTagName('body').click()")

None of them seem to work. All were performed by a sleeping thread and/or even an implicit wait

Url of page: https://www.mryum.com/sookthaikitchen/pickup/main-menu

2 Answers

This doesn't work because these elements don't receive the click.

driver.find_element_by_tag_name('body').click()
driver.find_element_by_tag_name('div').click()

Assuming your case, you want to get rid of this pop-up, the best approach would be to handle this pop-up like this.

driver.find_element_by_xpath('//button[contains(text(), "Thanks!")]').click()

Otherwise, you can always use ActionChains

Apparent approach if pop-ups are dynamic

from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver=driver) # your current chrome / Firefox driver
x = action.move_by_offset(100, 100).click()
x.perform()

It will be good answer in your case.

pip install autoit

import ait
ait.move(100, 100) //(x, y) is random point which does not contain button or a tag
ait.click()
Related