[英]How can I save the first link in google search automated by selenium?
[英]How can I save configuration on Google Preference by using Selenium?
如何单击“确定”(请参见屏幕截图)?
我使用Python 3.7,Selenium和Chrome作为浏览器。
如果要重现通知框,请转到https://www.google.com/preferences向下滚动到“区域设置”,选择一个区域,然后单击“保存”。
这是我的代码:
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"
看来,“确定”按钮没有XPATH
。
我还试图用WebDriverWait
和expected_conditions
以及driver.switch_to.alert
,但所有这些事情没有工作。
我不知道您之前尝试过什么代码,但这是我的看法
alertObject = driver.switch_to.alert
alertObject.accept()
经过几次尝试,它终于可以使用以下附加代码:
# 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()
在这里您可以找到有关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.