Skip to content
Advertisement

How to click button in pop up window using python-selenium

I’m working to automate web page where i’m unable to close the pop up. I have tried to refresh/switch to pop up window, nothing worked.

Code:

from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='F:chromedriver_win32chromedriver.exe')
driver.get('https://www.libertymutual.com/get-a-quote')
driver.maximize_window()
driver.find_element_by_xpath(
    '//*[@id="1555468516747"]/section/div[2]/section/form/div[1]/div[3]/div/div[1]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="zipcode-1555468516747-1555468516747"]').click()
driver.find_element_by_xpath('//*[@id="zipcode-1555468516747-1555468516747"]').send_keys('03878')
time.sleep(1)
driver.find_element_by_xpath(
    '//*[@id="1555468516747"]/section/div[2]/section/form/div[2]/div[5]/div/div/button').submit()
time.sleep(5)
x=driver.find_element_by_xpath('//*[@id="discount-marketing-modal"]/header/button/svg').click()
driver.refresh()

If you want directly go to web page, https://buy.libertymutual.com/auto?city=Somersworth&jurisdiction=NH&lob=Auto&policyType=Auto&zipCode=03878

Advertisement

Answer

Replace last 3 lines of your code by below lines.Used action chain to click.

time.sleep(5)
ok =  driver.find_element_by_xpath('//*[@id="discount-marketing-modal"]/footer/button')
ActionChains(driver).move_to_element(ok).pause(1).click(ok).perform()
x=driver.find_element_by_xpath('//*[@id="discount-marketing-modal"]/header/button/svg').click()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement