简体   繁体   English

如何使用Selenium在Google偏好设置上保存配置?

[英]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. 我使用Python 3.7,Selenium和Chrome作为浏览器。

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". 如果要重现通知框,请转到https://www.google.com/preferences向下滚动到“区域设置”,选择一个区域,然后单击“保存”。

在此处输入图片说明

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 . 看来,“确定”按钮没有XPATH
I also tried to use WebDriverWait and expected_conditions as well as driver.switch_to.alert , but all those things didn't work. 我还试图用WebDriverWaitexpected_conditions以及driver.switch_to.alert ,但所有这些事情没有工作。

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 在这里您可以找到有关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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 selenium 自动保存谷歌搜索中的第一个链接? - How can I save the first link in google search automated by selenium? 如何使用 selenium 提取 class 值并保存在 csv 中? - How Can I extract class value and save in csv using selenium? 如何使用Selenium在PhantomJS中设置浏览器首选项 - How to set browser preference in PhantomJS using Selenium 如何使用 selenium 保存密码(登录信息),所以我不必在 Instagram 上一次又一次地登录 - how can I save password(login info) using selenium , so I have to not login again and again on Instagram 如何使用 python 3 编辑和保存 azure function 应用程序的配置环境变量? - How can I edit and save configuration env variable of a azure function app using python 3? 如何使用带有 Selenium 的 Headless Google Chrome 保存手机屏幕截图 - How to save mobile screenshot using Headless Google Chrome with Selenium 如何使用 docker-compose 在容器中保存来自 selenium webdriver 的文件? - How can I save file from selenium webdriver in conteiner using docker-compose? 我如何使用 Selenium “保存并退出”cookie 弹出窗口 - How I can "save & exit" cookies popup with Selenium 如何在没有驱动器 API 的情况下使用 selenium 和 python 登录谷歌驱动器 - How can I login to google drive using selenium & python WITHOUT Drive API 我无法使用 python selenium 下载谷歌图片 - I can't download google images using python selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM