[英]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. 我还试图用
WebDriverWait
和expected_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.