Selenium Webdriver - how to close the first pop up window and go to the actual page

旧巷老猫 提交于 2021-02-19 04:13:54

问题


I'm trying to automate the webpage "http://www.quikr.com",when I open this you will get a pop up window first saying "Please Choose Your Location" then after closing it , I can see the main page of quikr.

I tried closing that Popup page by automation ,but not able to do

Tried using xpath

 driver.findElement(By.xpath("//*[@id='csclose']/strong")).click();

Tried using className

 driver.findElement(By.className("cs-close cs-close-v2")).click();

Tried using id

  driver.findElement(By.id("csclose")).click();

Please help me with this


回答1:


to close multiple popups in webdriver and switch to parent window

String parent = driver.getWindowHandle();

        Set<String> pops=driver.getWindowHandles();
        {
        Iterator<String> it =pops.iterator();
        while (it.hasNext()) {

            String popupHandle=it.next().toString();
            if(!popupHandle.contains(parent))
            {
            driver.switchTo().window(popupHandle);
            System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
            driver.close();



回答2:


The Following Code Works for Me to Handle Pop Up/ Alerts in Selenium Webdriver Just Copy Paste this Code After the Event which is triggering the Pop up/Alert i.e after clicking on save.

if(driver.switchTo().alert() != null)
{
    Alert alert = driver.switchTo().alert();
    String alertText = alert.getText();
    alert.dismiss(); // alert.accept();

}

in your case you try to run this code at starting of the code bcz it will directly close the pop up




回答3:


Since this is a JavaScript modal, when the page finishes loading the JavaScript code could still be running. The solution is to wait until the button to close the modal be displayed, close it and then follow with your test. Like this:

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        wait.Until(ExpectedConditions.ElementIsVisible(By.Id("csclose")));

        driver.FindElement(By.Id("csclose")).Click();

Tested myself and works fine.

Hope it helps.




回答4:


i have tried it in ruby and this one works see if this can help you in any way :)

require 'selenium-webdriver'
require 'test/unit'
require 'rubygems'

class Tclogin < Test::Unit::TestCase                                                        #------------ define a class----------------

    def setup
    @@driver = Selenium::WebDriver.for :firefox                                         
    @@driver.navigate.to "http://www.quikr.com"                 #---- call url----
    @@wait = Selenium::WebDriver::Wait.new(:timeout => 60) # seconds                     #----define wait------
    end

    def test_login
     @@driver.find_element(:css, "strong").click
    end
end

you can also use follwing xpath

@@driver.find_element(:xpath, "//a[@id='csclose']/strong").click



回答5:


public void closePopup() throws Exception {
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://www.quikr.com/");
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("csclose"))).click();
        System.out.println("Successfully closed the start Popup");
    }



回答6:


Try driver.findElement(By.Id("csclose")).click(); I hope that will help




回答7:


Simple pressing Alt + F4 buttons worked for me, e.g.:

        driver.findElement(By.cssSelector("html body div div img")).sendKeys(Keys.chord(Keys.ALT, Keys.F4));


来源:https://stackoverflow.com/questions/17465136/selenium-webdriver-how-to-close-the-first-pop-up-window-and-go-to-the-actual-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!