警报Python Selenium,Chrome WebDriver:切换至警报无效 [英] Alert Python Selenium, Chrome WebDriver: switch to alert doesn't work
本文介绍了警报Python Selenium,Chrome WebDriver:切换至警报无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关闭警报不起作用.每次使用python并使用chrome webdriver时,都会出现错误.传递登录名和密码可以正常工作.
Dismissing the alert doesn't work. I get an error every time, I'm using python and working with chrome webdriver. Passing login and password works fine.
我非常感谢您的帮助:)谢谢:D
I would really appreciate some help:) Thanks:D
from selenium import webdriver
import time
browser = webdriver.Chrome('My chromedriver path')
a = 1
while a == 1:
try:
browser.get('https://www.facebook.com/')
time.sleep(2)
l = browser.find_element_by_id('email')
l.send_keys('myphonenumber')
l = browser.find_element_by_id('pass')
l.send_keys('myspassword')
l = browser.find_element_by_id('loginbutton')
l.click()
time.sleep(7)
#code works to here and then breaks with error
b = browser.switch_to.alert()
b.dismiss()
time.sleep(2)
l = browser.find_element_by_xpath('//*[@id="js_9p"]/div/div/ul/li[1]/a')
time.sleep(2)
l.click()
except:
print('error occured, trying again')
这是我得到的错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException:
Message: no such alert
(Session info: chrome=80.0.3987.149)
推荐答案
尝试使用 chrome_options
,它将取消通知.
Try using chrome_options
, it will suppress the notification.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = webdriver.ChromeOptions()
preferences = {"profile.default_content_setting_values.notifications" : 2}
options.add_experimental_option("prefs", preferences)
browser = webdriver.Chrome(chromedriver, chrome_options=options)
browser.get('https://www.facebook.com/')
time.sleep(2)
l = browser.find_element_by_id('email')
l.send_keys('username')
l = browser.find_element_by_id('password')
l.send_keys('password')
l = browser.find_element_by_id('loginbutton')
l.click()
time.sleep(7)
这篇关于警报Python Selenium,Chrome WebDriver:切换至警报无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文