【问题标题】:Selenium dismiss/accept Chrome confirmation popup windowSelenium 关闭/接受 Chrome 确认弹出窗口
【发布时间】:2020-09-04 06:35:46
【问题描述】:

我目前正在自动化日常流程之一,并且我已经到了出现 chrome 弹出窗口的地步:

我尝试过:

alert = browser.switch_to.alert
alert.dismiss()

堆栈跟踪:

C:\...>python "C:\\...\\script.py" username pwd code

DevTools listening on ws://127.0.0.1:61324/devtools/browser/464b40f5-dc5b-4fec-be25-35fdadb765e5
[14564:15520:0904/092015.725:ERROR:device_event_log_impl.cc(208)] [09:20:15.725] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
Traceback (most recent call last):
  File "C:\...\script.py", line 53, in <module>
    alert = browser.switch_to.alert
  File "C:\...\Local\Programs\Python\Python38-32\lib\site-packages\selenium-3.141.0-py3.8.egg\selenium\webdriver\remote\switch_to.py", line 55, in alert
    alert.text
  File "C:\...\Local\Programs\Python\Python38-32\lib\site-packages\selenium-3.141.0-py3.8.egg\selenium\webdriver\common\alert.py", line 67, in text
    return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
  File "C:\...\Local\Programs\Python\Python38-32\lib\site-packages\selenium-3.141.0-py3.8.egg\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\...\Local\Programs\Python\Python38-32\lib\site-packages\selenium-3.141.0-py3.8.egg\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
  (Session info: chrome=85.0.4183.83)

我尝试过使用time.sleep(2),但没有帮助。

还有其他方法可以检测到这个窗口吗?或者在这种情况下,我们可以传递 ChromeOptions 允许打开启动器,从而阻止此窗口。

感谢任何帮助。

我的代码(我是初学者):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC

import time
import sys

url = "link"
argv = sys.argv[1:]

if argv and len(argv) == 3:

    browser = webdriver.Chrome(executable_path="...\\chromedriver.exe")
    browser.get(url)
    time.sleep(2)

    if browser.title == "the_title":

        # Login

        time.sleep(2)
        
        username = browser.find_element_by_name("field1")
        password = browser.find_element_by_name("field2")
        security_code = browser.find_element_by_name("field3")

        username.send_keys(sys.argv[1])
        password.send_keys(sys.argv[2])
        security_code.send_keys(sys.argv[3])

        submit = browser.find_element_by_id("submit")

        submit.click()

        # Detect

        time.sleep(5)
        detect = browser.find_element_by_id("detect_button")
        detect.click()

        time.sleep(2)

        # stuck at this point
        alert = browser.switch_to.alert
        alert.dismiss()
        #WebDriverWait(browser, 5).until(lambda d: browser.switch_to_window(str(d.window_handles)))

        # steps after
        ...

    else:
        print("Failed to load the page")

else:
    print("Invalid/Incorrect arguments passed. Provided arguments: " + str(argv))

browser.quit()

【问题讨论】:

  • 在没有切换到警报的情况下运行代码时遇到了什么错误?如果您收到除 UnhandledAlert 之外的任何错误,则它不是警报。
  • @rahulrai 我没有收到任何错误
  • 那么它就不是警报窗口了。现在检查此窗口并在元素选项卡中搜索 //iframe。看看有没有匹配的。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

接受警告框的正确方法是:

browser.switch_to.alert.accept()

【讨论】:

  • 我仍然得到selenium.common.exceptions.NoAlertPresentException: Message: no such alert 和你的代码。
  • 那好像是webdriver连警告框都识别不出来
  • 会不会连alertbox类型都没有?
  • 很可能是这样
【解决方案2】:

在网页上,每当警报出现时,我们需要检查它是否真的是警报,或者它只是页面本身的一部分。您可以通过检查警报来检查,

如果警报不允许您检查它,则表示它处于警报状态。在这种情况下,

alert = browser.switch_to.alert
alert.dismiss()

这些命令运行,如果警报需要时间,那么在这里使用显式等待

例如:

alert = wait.until(EC.alert_is_present())
print(alert.text)
# alert.accept()
alert.dismiss() 

只要导入 from selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWait 就可以了

但是如果您能够检查它并查找它具有的属性,这意味着它不是警报,您必须像处理其他元素一样处理页面的一部分。 根据您收到的错误NoAlertPresentException,这清楚地表明没有警报。

【讨论】:

    【解决方案3】:

    无法在 selenium 中将对话框作为 javascript 处理 - 它不是网页的一部分,而是 chrome GUI(系统对话框)。这就是您收到 NoAlertPresentException 的原因。

    要跳过此类异常,您可以使用 try-except 块:

    try:
        alert = browser.switch_to.alert
        alert.dismiss()
    except NoAlertPresentException:
        print("There's no alert on the page...")
    

    这种方法可以让您毫无例外地继续进行页面处理。

    要处理系统对话框,您可以使用一些 GUI 模块,如 PyAutoGUIwin32api+win32con(如 win32api.mouse_event(win32con..MOUSEEVENTF_LEFTDOWN, x, y))等。以 PyAutoGUI 为例,在全尺寸 chrome 浏览器上单击确认下载对话框的按钮窗口:

    import pyautogui as pag  
    ...
    try:
        alert_obj = driver.switch_to.alert
        alert_obj.accept()
    except NoAlertPresentException:
        w, h = pag.size()
        pag.moveTo(5*w//9, h//5)
        pag.click()
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多