Gabrielius B.
Gabrielius B.

Reputation: 81

How to close pop up window in Selenium

Can't close pop up window which appears right after http://www.cargo.lt/ loads. Here's what I've got:

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
time.sleep(10)
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()

I'm not very familiar with how to write custom xpath/css path and now just clicked on Inspect element and copied xpath. What I'm doing wrong?

EDIT: What a stupid mistake. Didn't realize that when element is off screen Selenium can't click on it. Just added driver.maximize_window() and all my problems are gone. Thanks all for your answers. Unfortunately I can't vote yet, because I don't have enough points...

Upvotes: 2

Views: 10730

Answers (4)

yunus
yunus

Reputation: 68

you can use chrome options

in Python, you can use

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
webdriver.Chrome(os.path.join(path, 'chromedriver'),chrome_options=chrome_options)

in java

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("test-type");
ChromeDriver driver = new ChromeDriver(options);

Upvotes: 0

FRe34
FRe34

Reputation: 117

i tried your code it's work fine i don't know whats you problem but try this : driver.implicitly_wait(10) insted of :

time.sleep(10)

Upvotes: 2

Strik3r
Strik3r

Reputation: 1057

You can use the Predefined commands in python -Selenium to switch to alert box.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
alrt = driver.switch_to_alert()
alrt.accept()

Hope it Helps.

Upvotes: 0

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

hi to close pop up /alert please use

driver.switch_to_alert()

then use

driver.find_element_by_xpath('/html/body/div[36]/div/a').click()

// if u have copied pasted form firebug then it will be correct i guess

or if ur xpath is not correct then use

driver.find_element_by_xpath("//a[@id='advert_x']").click()

Upvotes: 2

Related Questions