【问题标题】:Selenium - eCLIPSE - WebDriver Alert Handling - QuerySelenium - eCLIPSE - WebDriver 警报处理 - 查询
【发布时间】:2017-07-18 02:20:04
【问题描述】:

在 Eclipse 中,我打算自动执行以下步骤 (1) 打开谷歌浏览器 (2) 输入https://www.flipkart.com/ (3) 最大化窗口 (4)(几秒钟后),网站显示一个新的登录窗口。 (5) 只需使用右上角的“X”图标关闭弹出窗口即可。

在代码中将警报窗口命名为尖锐,但

关于代码 Alert Sharp = Flip.switchTo().alert();收到警告信息为“未使用局部变量Sharp的值”

在执行时,不执行第 4 步和第 5 步。已经使用了 Sharp.dismiss();也使用过,但仍然没有失败。

为此编写了以下代码。请澄清

package basic;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.Alert;

public class FlipKart{

    public static void main(String[]args) throws InterruptedException{

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\hp\\workspace\\Selenium\\browser\\chromedriver.exe"); 

    WebDriver Flip = new ChromeDriver();
    Flip.get("https://www.flipkart.com/");
    Flip.manage().window().maximize();
    Thread.sleep(5000); //FLIPKART CODE
    Alert Sharp = Flip.switchTo().alert();// pop -up window shown

//点击右上角的X图标关闭窗口。

Flip.findElement(By.xpath("/html/body/div[2]/div/div/button")).click(); 
    }

}

【问题讨论】:

    标签: eclipse selenium webdriver


    【解决方案1】:

    Flipkart 的登录部门是一个单独的部门,既不包含在警报中,也不包含在弹出窗口/框架中。因此,只需使用 xpath 定位元素 [X] 并单击它即可关闭登录窗口。 下面的简单语句对我有用。

    删除

    Thread.sleep(5000); //FLIPKART CODE
        Alert Sharp = Flip.switchTo().alert();
    

    保留

    driver.findElement(By.xpath("/html/body/div[3]/div/div/button")).click();
    

    【讨论】:

    • driver.findElement(By.xpath("/html/body/div[3]/div/div/button")).click();不工作。 selenium 无法识别弹出窗口。错误消息显示不存在具有给定 xpath 的 Web 元素。
    【解决方案2】:

    这是您问题的答案:

    正如您提到的the website shows a new pop-up window to login. just close the pop-window using the "X" icon at the top right corner,如果您尝试通过Development Tools 定位元素,则弹出窗口源于HTML DOM。因此,当您尝试 Alert Sharp = Flip.switchTo().alert(); 时,我们将无法通过 Alert 类处理此弹出窗口。相反,我们将通过WebDriver 关闭弹出窗口,如下所示:

    值得一提的是,弹出窗口会在一段时间后出现,因此我们应该诱导一些ExplicitWait,即WebDriverWait,以便弹出窗口出现。

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Q45129845_Flipkart_Login_Close 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            WebDriver Flip = new ChromeDriver(options);
            Flip.get("https://www.flipkart.com/");
            WebDriverWait wait = new WebDriverWait(Flip, 20);
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='_2AkmmA _29YdH8']")));
            element.click();
        }
    }
    

    如果这能回答您的问题,请告诉我。

    【讨论】:

      【解决方案3】:

      尝试使用下面的代码 -

      driver.findElement(By.xpath("/html/body/div[2]/div/div/button")).click();
      

      编辑

      wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[2]/div/div/button"))).click();
      

      【讨论】:

      • 只有 xpath 应该这样做。等待警报将抛出 TimeoutExcpetion,因为页面源不涉及警报。
      • @Mkay - 是的,你是对的。我正在更新我的答案。谢谢
      • driver.findElement(By.xpath("/html/body/div[3]/div/div/butto‌​n")).click();不工作。 selenium 无法识别弹出窗口。错误消息显示不存在具有给定 xpath 的 Web 元素
      • 正如你提到的网站显示一个新的登录弹出窗口,我检查了它并没有这样的窗口出现。为此,您将不得不引入一些 ExplicitWait。更新我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多