简体   繁体   中英

Get text element from a javascript alert using webdriver

I'm using selenium webdrive in c# for learning and writing automated tests and i have come across this situation:

Say you have a website like this: http://referencewebapp.qaautomation.net/register.php and you get the pop-up with the error message as a javascript alert(if you press Register without filling in any info, for example). What i need to do is to check what error message (the text) is in the alert.

I cannot use driver.FindElement or drive.PageSource as i cannot see the source code for it. I read about trying to select/change the frame( Selenium cannot get alert thrown in iframe ), but i don't know how the frameID. Is there any way i can find and check that text?

Thanks for any suggestions.

 driver.SwitchTo().Alert().Text;

应该是您所需要的。

You can try to get the text of an alert (dialog) by using selenium Alert object.

for example:

    Alert alert = driver.switchTo().alert();
    String alertText = alert.getText();
    if (alertText.equals("error...")) {

        // Do Something...

        // dismiss alert
        alert.dismiss();

    }

More information in the selenium 2.0 documentation.

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