问题
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.
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')
This is the error I get:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException:
Message: no such alert
(Session info: chrome=80.0.3987.149)
回答1:
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)
来源:https://stackoverflow.com/questions/60908796/alert-python-selenium-chrome-webdriver-switch-to-alert-doesnt-work