【问题标题】:Unable to click on the selenium text alert无法单击硒文本警报
【发布时间】:2021-07-27 23:22:38
【问题描述】:

我正在尝试单击 selenium 中的文本警报,但是当我使用单击功能时,它无法识别该命令。

我的代码如下:

chrome.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/table[1]/tbody/tr[14]/td[1]/input[1]').click()

sleep(3)

alert = WebDriverWait(s.chrome, 3).until(EC.alert_is_present(),"Confirma a 
operação?")

alert.accept

print("alert accepted")

这是我要点击的页面元素:

input type="SUBMIT" name="BTN_ENTER" value="Confirmar" class="Button" gxevent="EENTER." onclick="if( confirm( 'Confirma a operação?')) {GX_setgridevent( 45,  12);GX_setevent('EENTER.');} else return false;" gxctx="_" onfocus="gxonfocus2(this, 59,'',45)" gxoldvalue="Confirmar"

我看到在我配置它之后,它有一个名为one click 的选项,但我不知道我必须设置什么值才能让它识别元素。

【问题讨论】:

    标签: javascript python selenium visual-studio-code webdriver


    【解决方案1】:

    当您检查alert_is_present() 的内部实现时,在内部处理switch_to.alert,以便我们可以处理警报

    chrome.find_element_by_xpath("//input[@name='BTN_ENTER']").click()
    
    WebDriverWait(chrome, 3).until(EC.alert_is_present(),'Confirma a operação?').accept()
    print("alert accepted")
    

    更新

    根据UnexpectedAlertPresentException,您可以按以下方式处理它

    chrome_options = Options()
    chrome_options.set_capability('unhandledPromptBehavior', 'accept')
    driver = webdriver.Chrome(options=chrome_options)
    

    【讨论】:

    • 我测试了这段代码,出现如下错误:self.error_handler.check_response(response) File "C:\Users\Luis.Serpa\anaconda3\lib\site-packages\selenium\webdriver\remote \errorhandler.py",第 241 行,在 check_response 中引发 exception_class(message, screen, stacktrace, alert_text) selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:确认操作?消息:意外警报打开:{警报文本:确认操作?}(会话信息:chrome=92.0.4515.107)
    • 好的,所以它显示 UnexpectedAlertPresentException
    • 我可以点击确认,但是当我点击确认时,它会发出文本警报,但它无法下载报告。
    • 好的,所以警报是应用程序的预期行为?你能检查我是否更新了我的答案
    • 知道了,我删除了:chrome.find_element_by_xpath("//input[@name='BTN_ENTER']").click() 他直接确认了,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2017-10-20
    • 2018-11-21
    相关资源
    最近更新 更多