我目前正在使日常流程之一自动化,并且到了出现chrome弹出窗口的地步:
我试图:
alert = browser.switch_to.alert
alert.dismiss()
Stacktrace:
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()
答案 0 :(得分:0)
接受警报框的正确方法是:
browser.switch_to.alert.accept()
答案 1 :(得分:0)
每当警报到来时,在网页上,我们都需要检查它实际上是警报还是只是页面本身的一部分。您可以通过检查警报来进行检查,
如果警报不允许您检查它,则表示它是警报。在这种情况下,
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 EC
和from selenium.webdriver.support.wait import WebDriverWait
时起作用
但是如果您能够检查它并查找它具有的属性,这意味着它不是警报,则必须像处理其他元素一样处理页面的一部分。
根据您收到的错误NoAlertPresentException
,这显然表明没有警报。