stefan
stefan

Reputation: 5068

How can I save configuration on Google Preference by using Selenium?

How can I click on "OK" (see screenshot)?

I use Python 3.7, Selenium and Chrome as browser.

If you want to reproduce the notification box, go to https://www.google.com/preferences scroll down to "Region Settings" choose a region and click on "Save".

enter image description here

Here's my code:

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument("--lang=en")
options.add_argument("--disable-notifications")

driver = webdriver.Chrome(options=options)
driver.implicitly_wait(30)

driver.get('https://www.google.com/preferences#languages')
driver.find_element_by_xpath('//*[@id="langten"]/div/div').click()
driver.find_element_by_xpath('//*[@id="form-buttons"]/div[1]').click()
time.sleep(1)

driver.get('https://www.google.com/preferences')
driver.find_element_by_xpath('//*[@id="regionanchormore"]/span[1]').click()
driver.find_element_by_xpath('//*[@id="regionoUS"]/div/div').click()
driver.find_element_by_xpath('//*[@id="form-buttons"]/div[1]').click()
time.sleep(1)

# Now I need to click on "OK"

It seems, that the "OK" button doesn't have a XPATH.
I also tried to use WebDriverWait and expected_conditions as well as driver.switch_to.alert, but all those things didn't work.

Upvotes: 0

Views: 761

Answers (3)

stefan
stefan

Reputation: 5068

After several tries, it finally work with this additional piece of code:

# Now I need to click on "OK"
try:
    WebDriverWait(driver, 3).until(EC.alert_is_present(),'Timed out waiting alert to appear!')

    alert = driver.switch_to.alert
    alert.accept()
    print("alert accepted")
except TimeoutException:
    print("no alert")

driver.quit()

Here you can find the doc regarding Alerts: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.alert

Upvotes: 0

user2669905
user2669905

Reputation: 11

use firefox...recreate the same situation and find the Xpath (or id)of that button and do something like this: IWebElement okbtn = d.FindElement(By.Xpath("insert xpath here"));

okbtn.Click();

Upvotes: -1

hyperbeam
hyperbeam

Reputation: 46

I don't know what code you were trying before this, but here's my take on this

alertObject = driver.switch_to.alert
alertObject.accept()

Upvotes: 3

Related Questions