简体   繁体   中英

Unable to click on the selenium text alert

I'm trying to click on the text alert in selenium but when I use the click function it doesn't recognize the command.

My code is as follows:

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")

This is the page element I'm trying to click:

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"

I saw that there after I configure it, it has an option called one click but I don't know what value I have to set for it to recognize the element.

When you check the internal implementation of alert_is_present() internally handle the switch_to.alert so we can handle the 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")

Update

As per the UnexpectedAlertPresentException you can handle it as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM