如何关闭Selenium Web驱动程序中的弹出窗口?

时间:2018-11-03 12:49:59

标签: java selenium-webdriver

我试图用Selenium关闭此弹出页面,但无法完成。

弹出窗口中X按钮的HTML代码:

<div class="adoric_element element-shape closeLightboxButton" style="left: -28px; top: 0px; position: absolute; display: flex; flex-direction: column; width: 17px; height: 18px; line-height: normal; opacity: 1; cursor: pointer; z-index: 104;" data-width="18" data-height="20" data-left="34" data-top="-54" data-radius="0.00"><!--?xml version="1.0" encoding="utf-8"?--> <!-- Generated by IcoMoon.io -->  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 500 500" class="inner-element" preserveAspectRatio="none" style="width: 100%; height: 100%; opacity: 1; box-shadow: none; border-radius: 0px;" data-event-name="Conversion" data-shadow-distance="0" data-width="NaN" data-height="NaN" data-left="NaN" data-top="NaN" data-base-bg-color="rgb(255, 255, 255)"> <path d="M492.561 457.327c9.729 9.758 9.729 25.516 0 35.304-4.879 4.919-11.268 7.359-17.677 7.359s-12.788-2.439-17.637-7.359l-207.327-207.317-207.287 207.327c-4.879 4.919-11.278 7.359-17.677 7.359s-12.798-2.439-17.637-7.359c-9.758-9.829-9.758-25.596 0-35.304l207.287-207.367-207.287-207.337c-9.758-9.789-9.758-25.556 0-35.314s25.596-9.758 35.354 0l207.247 207.327 207.287-207.327c9.758-9.758 25.596-9.758 35.344 0 9.769 9.758 9.769 25.516 0 35.314l-207.277 207.327 207.287 207.367z" fill="rgb(255, 255, 255)" style=""></path> </svg> </div>

我的脚本

package com.Selenium_for_intelliJ;

    import org.openqa.selenium.By;
    import org.openqa.selenium.Point;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import java.util.List;
    import org.openqa.selenium.support.ui.WebDriverWait;



public static void main(String[] args) throws InterruptedException {
    System.out.println("Hello");

     // Chrome
    System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().setPosition(new Point(3000,1));
    driver.manage().window().maximize();

    driver.get("https://www.daka90.co.il");
    Thread.sleep(1000);

    // End2End script - Abroad Flight+Hotel

    driver.findElement(By.xpath("//div[@id='buttontab5']")).click();
    driver.findElement(By.xpath("//input[@id='fromLocation']")).click();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//input[@id='fromLocation']")).sendKeys("ישראל");
    Thread.sleep(3000);
    driver.findElement(By.xpath("//span[@data-realvalue='1218']")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//input[@id='toLocation']")).click();
    driver.findElement(By.xpath("//input[@id='toLocation']")).sendKeys("אמסטרדם");
    Thread.sleep(3000);
    driver.findElement(By.xpath("//input[@id='from2']")).click();
    Thread.sleep(1000);

    //stores all webelements of the days in List (35 objects) and click on 27th

    List<WebElement> dates = driver.findElements(By.xpath("//div[@class='ui-datepicker-group ui-datepicker-group-first']/table//td"));

    int total_node=dates.size();

    for(int i=0;i<total_node;i++)
    {
        String date=dates.get(i).getText();
        if(date.equals("27"))
        {
            dates.get(i).click();
            break;
        }
    }
    Thread.sleep(2000);

    dates = driver.findElements(By.xpath("//div[@class='ui-datepicker-group ui-datepicker-group-last']/table//td"));
    total_node=dates.size();

    for(int i=0;i<total_node;i++)
    {
        String date=dates.get(i).getText();
        if(date.equals("8"))
        {
            dates.get(i).click();
            break;
        }
    }

    Thread.sleep(2000);

    driver.findElement(By.xpath("//div[@id='personsSelect']")).click();
    Thread.sleep(2000);

    driver.findElement(By.id("submitSearch")).click();
    Thread.sleep(2000);

    //***** All my tries to close this pop-up by clicking the X button" *****
    //driver.close();
    // driver.findElement(By.xpath("//div[@class='adoric_element element-shape closeLightboxButton']")).click();
    //driver.findElement(By.xpath("//svg[@class='inner-element']")).click();
    //driver.findElement(By.xpath("//path[@d='M492.561 457.327c9.729 9.758 9.729 25.516 0 35.304-4.879 4.919-11.268 7.359-17.677 7.359s-12.788-2.439-17.637-7.359l-207.327-207.317-207.287 207.327c-4.879 4.919-11.278 7.359-17.677 7.359s-12.798-2.439-17.637-7.359c-9.758-9.829-9.758-25.596 0-35.304l207.287-207.367-207.287-207.337c-9.758-9.789-9.758-25.556 0-35.314s25.596-9.758 35.354 0l207.247 207.327 207.287-207.327c9.758-9.758 25.596-9.758 35.344 0 9.769 9.758 9.769 25.516 0 35.314l-207.277 207.327 207.287 207.367z']")).click();
    Thread.sleep(3000);

错误按摩

线程“ main”中的异常org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // div [@ class ='adoric_element元素-shape closeLightboxButton']“}   (会议信息:chrome = 70.0.3538.77)   (驱动程序信息:chromedriver = 2.43.600210(68dcf5eebde37173d4027fa8635e332711d2874a),平台= Windows NT 10.0.17134 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0毫秒

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该让Windows处理并切换到弹出窗口,然后执行关闭操作,下面我将与您分享两种不同的弹出窗口和提示方法:

windowHandles = driver.getWindowHandles(); //find pop-up windows in this list
driver.switchTo().window(windowName); //switching to selected window.
driver.findElement(closePopUpSelector).click(); 

driver.switchTo().alert().accept(); 
driver.switchTo().alert().dismiss();