简体   繁体   中英

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".

在此处输入图片说明

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.

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

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

使用firefox ...重新创建相同的情况并找到该按钮的Xpath(或id),然后执行以下操作:IWebElement okbtn = d.FindElement(By.Xpath(“在此处插入xpath”)));

okbtn.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