简体   繁体   中英

How to verify the text present in the modal dialog using Selenium Webdriver

I have a webpage which has a 'see Terms' option. On clicking 'see Terms' a modal dialog box opens. I want to verify the text in the modal dialog. The problem I am facing is that how to switch to modal dialog box from the main window. I tried the driver.getWindowHandles() but that dowsn't work. Please suggest how can I switch focus to dialog box and get it's text.

I am using Selenium 2.52.0 and java as a language.

在此处输入图片说明

It's not a separate window, so you shouldn't do anything with getWindowHandles(). Just try to assert by getting the text with browser.findElement(modal path).getText() on the same page.

HI simply use like below it will work NOTE please update your selenium version.

String contentOfTerms = driver.findElement(By.xpath("//*[@class='terms_content']/p")).getText();
System.out.println(contentOfTerms);

You need to switch window if it is in different frame but its does not look like a different frame although if it is in different frame the try and use following code which will switch parent to child window:

public void windowSwitch()
            {
                String parentWin = driver.getWindowHandle();    
            parentWin1=parentWin;
                Set<String> allwin =driver.getWindowHandles();
            for(String child : allwin)
            {
            if(!child.equals(parentWin))
                {
                driver.switchTo().window(child);
                    }
              }
            }

And if it is a dialog box only then use following code to get string from it:

        public String alert()
        {    String str;
            Alert alert =  driver.switchTo().alert();  
            str= alert.getText();
            alert.accept();
            return  str;

        }

driver.switchTo().activeElement(); ///to switch into active popupelement...

driver.findElement(By.xpath("//*[@class='terms_content']/p")).getText();//to get a text of a element you given itll work for you..

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