首页 > 解决方案 > 无法单击硒文本警报

问题描述

我正在尝试单击 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但我不知道我必须设置什么值才能识别元素。

标签: javascriptpythonseleniumvisual-studio-codewebdriver

解决方案


当您检查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)

推荐阅读