简体   繁体   中英

Selenium gets stuck during alert Python

I've encountered a problem with selenium + chrome-driver.

URL that I am working with: https://tixcraft.com/ticket/verify/23_ttp6th/12494 What I want to do is to accept the error alert when I intentionally send the wrong key. However, the code always gets stuck unless I manually accept it.

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

Any idea what could be the issue? Thanks?

I've tried manually keying random values in the input box and press Enter. The code detects the alert successfully that way.

I expect using Selenium to key in the values would be the same but for some reason it doesn't work.

This is because the alert box is outside of the web element whereby you need to switch the focus before accept() or dismiss() it.

You can achieve it by doing so:

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

You can read more about it here

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