【问题标题】:Selenium gets stuck during alert PythonSelenium 在警报 Python 期间卡住
【发布时间】:2023-01-11 00:55:52
【问题描述】:

我遇到了 selenium + chrome-driver 的问题。

我正在使用的 URL:https://tixcraft.com/ticket/verify/23_ttp6th/12494 我想做的是在我故意发送错误密钥时接受错误警报。但是,代码总是卡住,除非我手动接受它。

        veri_box = self.std_wait.until(
            EC.element_to_be_clickable((By.ID, 'checkCode'))
        )
        veri_box.send_keys("randomKey")
        veri_box.send_keys(Keys.ENTER)

        #the code gets stuck here unless I manually click OK on the alert.        
        self.std_wait.until(EC.alert_is_present()).accept()

知道可能是什么问题吗? 谢谢?

我试过在输入框中手动键入随机值并按 Enter 键。代码以这种方式成功检测到警报。

我希望使用 Selenium 键入值是相同的,但由于某种原因它不起作用。

【问题讨论】:

  • 顺便说一句,self.std_wait = WebDriverWait(self.driver, 1)

标签: python selenium


【解决方案1】:

这是因为警告框在 Web 元素之外,因此您需要在 accept()dismiss() 之前切换焦点。

您可以通过这样做来实现它:

WebDriverWait(driver, 10).until(EC.alert_is_present())
alert_box = driver.switch_to.alert
alert_box.dismiss()

你可以阅读更多关于它的信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多