在 python 中使用 selenium 处理电子邮件弹出提示
Handling email pop-up prompt using selenium in python
我不知道如何在 Python 中解决这个问题。电子邮件弹出窗口阻止 Selenium 单击其中一个页脚链接,因为弹出窗口阻止了它的视图。理想情况下,我想单击 "X" 而不是输入电子邮件。
我试过使用 Selenium 文档中关于提示的内容,但 none 它起作用了,或者我可能实施不正确。我尝试了一些我已经在堆栈溢出中发现的东西,你可以在注释掉的代码中看到,但不断出现各种错误。
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
url = "https://www.standardmedia.co.ke/"
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source.encode('utf-8')
page_num = 0
##options = Firefox_options();
##options.addPreference("dom.disable_beforeunload", true)
##driver = webdriver.Firefox(options);
#click on the headings in the footer
for i in range (0,1):
footer = driver.find_elements_by_css_selector('#top-footer li')[i]
## if(driver.switch_to.alert != null):
## alert = driver.switch_to.alert
## alert.dismiss()
try:
WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.dismiss()
print("Alert dismissed.")
except TimeoutException:
print("No alert.")
footer.click()
print("alert dismissed")
page_num += 1
subheadings = driver.find_elements_by_css_selector('.title a')
len(subheadings)
Firefox 网络驱动程序的最新错误是 "No connection could be made because the target machine actively refused it."
除了 selenium 连接问题,为了解决主要问题,您似乎没有调用 switch_to_alert
函数。连接 selenium 后,请尝试以下操作:
alert = driver.switch_to_alert()
alert.dismiss()
另请注意,您的等待时间以 秒 为单位,这在您的代码中看起来很漂亮 high/long。
WebDriver Alert class 旨在与 JavaScript 警报配合使用:
在您尝试测试的页面上,此模态弹出窗口是正常的 <div>
因此您将无法使用警报 class,您通常必须使用 find_element() 功能找到关闭按钮并单击它。
-
driver.get("https://www.standardmedia.co.ke/")
-
popup = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, "mc-closeModal")))
-
popup.click()
我不知道如何在 Python 中解决这个问题。电子邮件弹出窗口阻止 Selenium 单击其中一个页脚链接,因为弹出窗口阻止了它的视图。理想情况下,我想单击 "X" 而不是输入电子邮件。
我试过使用 Selenium 文档中关于提示的内容,但 none 它起作用了,或者我可能实施不正确。我尝试了一些我已经在堆栈溢出中发现的东西,你可以在注释掉的代码中看到,但不断出现各种错误。
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
url = "https://www.standardmedia.co.ke/"
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source.encode('utf-8')
page_num = 0
##options = Firefox_options();
##options.addPreference("dom.disable_beforeunload", true)
##driver = webdriver.Firefox(options);
#click on the headings in the footer
for i in range (0,1):
footer = driver.find_elements_by_css_selector('#top-footer li')[i]
## if(driver.switch_to.alert != null):
## alert = driver.switch_to.alert
## alert.dismiss()
try:
WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.dismiss()
print("Alert dismissed.")
except TimeoutException:
print("No alert.")
footer.click()
print("alert dismissed")
page_num += 1
subheadings = driver.find_elements_by_css_selector('.title a')
len(subheadings)
Firefox 网络驱动程序的最新错误是 "No connection could be made because the target machine actively refused it."
除了 selenium 连接问题,为了解决主要问题,您似乎没有调用 switch_to_alert
函数。连接 selenium 后,请尝试以下操作:
alert = driver.switch_to_alert()
alert.dismiss()
另请注意,您的等待时间以 秒 为单位,这在您的代码中看起来很漂亮 high/long。
WebDriver Alert class 旨在与 JavaScript 警报配合使用:
在您尝试测试的页面上,此模态弹出窗口是正常的 <div>
因此您将无法使用警报 class,您通常必须使用 find_element() 功能找到关闭按钮并单击它。
-
driver.get("https://www.standardmedia.co.ke/")
-
popup = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, "mc-closeModal")))
-
popup.click()