【问题标题】:How to close Popovers and in-line ads with selenium webdriver如何使用 selenium webdriver 关闭弹出框和内嵌广告
【发布时间】:2017-02-02 17:40:45
【问题描述】:

我正在尝试使用在线教程等学习 Selenium Webdriver...

我正在努力克服这个关闭这个弹出窗口的障碍。


使用: 笔记本电脑:外星人 操作系统:Windows 10 64 位 浏览器:Firefox 51.0.1(32 位) Eclipse:版本:Neon.2 Release (4.6.2) Build id:20161208-0600 Selenium Webdriver:Java 3.0.1 2016-10-18


`package com.indeed.tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class IndeedJobSearch {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        //Create firefox driver to drive the browser

        System.setProperty("webdriver.gecko.driver", "C:\\Users......\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

        //Open Indeed home page
        driver.get("https://www.indeed.co.uk/");
        //Find what field and enter Selenium 
        Thread.sleep(1000);
        driver.findElement(By.id("what")).sendKeys("Selenium");
        //Find location field and enter London  
        driver.findElement(By.id("where")).clear();
        Thread.sleep(1000);
        driver.findElement(By.id("where")).sendKeys("London");
        //Find FindJobs button and click on it
        Thread.sleep(1000);
        driver.findElement(By.id("fj")).click();
        //Close popup - popover, not popup
            //prime-popover-div 
            //selenium webdriver cannot close bootstrap popovers
        //Can't find a solution



        //From job search results page, get page title and jobs count message
        //searchCount

        System.out.println(driver.getTitle());
        System.out.println(driver.findElement(By.id("searchCount")).getText());

        driver.close();
    }

}

`


预期结果:Selenium Webdriver 将打开 Firefox 浏览器,加载 Indeed.co.uk 网页,在第一个字段中插入“Selenium”,在第二个字段中插入“London”,点击搜索按钮,在控制台和驱动程序窗口中获取标题和作业计数值。

实际结果: Selenium Webdriver 会打开 firefox 浏览器,加载 Indeed.co.uk 网页,在第一个字段中插入“Selenium”,在第二个字段中插入“London”,点击搜索按钮, 停止对 url 字段的关注,没有其他任何事情发生。


我尝试了一些解决方案,但无法正常工作 (https://sqa.stackexchange.com/questions/5310/how-to-close-pop-up-window-in-selenium-webdriver)

例如


driver.findElement(By.id("prime-popover-close-button")).click();

Driver.SwitchTo().frame("prime-popover-div");

Driver.findElement(By.id("prime-popover-close-button")).click();

Driver.SwitchTo().defaultContent();

driver.findElement(By.xpath("//*[@id='prime-popover-close-button']/a/img")).click();

注意:不完全确定我的 xpath 是否正确编写,仍在学习中。

这些似乎都不起作用。我读到一些关于 Selenium WebDriver 不处理引导弹出窗口的内容,不确定这是否正是我的情况,或者你们中的任何人是否找到了解决方案。

希望有解决方案和/或建议:)

非常感谢您。

【问题讨论】:

    标签: java selenium selenium-webdriver popover bootstrap-popover


    【解决方案1】:

    您的代码通常看起来不错(除了使用Thread.Sleep(),我将在稍后解决。

    在这些情况下,您要做的基本上是右键单击对话框的关闭 X 并将其视为页面上的任何其他元素。找到 X 的定位器,在这种情况下,它还有一个 id,prime-popover-close-button,我们可以使用它。您需要做的就是使用 id 获取该元素并单击它以关闭弹出窗口。我已经简化了下面的代码。

    driver.get("https://www.indeed.co.uk/");
    driver.findElement(By.id("what")).sendKeys("Selenium");
    driver.findElement(By.id("where")).sendKeys("London");
    driver.findElement(By.id("fj")).click();
    driver.findElement(By.id("prime-popover-close-button")).click();
    

    如果您不尝试在搜索页面上测试 UI(输入文本和单击按钮),您可以直接导航到该 url,如果您愿意,甚至可以输入您自己的关键字。请参阅下面的代码。

    String what = "selenium";
    String where = "london";
    driver.get("https://www.indeed.co.uk/jobs?q=" + what + "&l=" + where);
    driver.findElement(By.id("prime-popover-close-button")).click();
    

    现在回到Thread.Sleep()。这种形式的等待通常是一种不好的做法。您可以对细节进行一些研究,但只要说它不灵活就足够了。如果你睡了 10 秒并且元素在 25 毫秒内出现,那么你已经等了很长时间,而你不需要。阅读WebDriverWaitExpectedConditions。虽然您在这里不需要它,但您最终需要等待,这些是等待的最佳做法。

    【讨论】:

    • 您好@JeffC,我确实尝试过,正如我之前在问题上提到的那样......尝试了一些解决方案但没有奏效。 Yogesh Rathi 和 BurritoBandit 有一个很好的观点,我所要做的就是添加一个等待弹出框可用然后单击。 (我之前没想过......我承认我还是很新)。无论如何,感谢您对睡眠线程的建议,我会阅读更多内容。 :)
    • 我发布的代码是工作代码。我自己试过了,效果很好。
    • 你好@JeffC,在我在这里发布问题之前,我已经尝试过了。我尝试了很多解决方案,但都没有奏效。即使是现在,我也创建了一个 NewJavaProject 来再次尝试您的代码。就我而言,它不起作用。但是如果我有等待直到预期的条件,那么其余的代码就可以工作了。 "import org.openqa.selenium.support.ui.WebDriverWait;""WebDriverWait wait = new WebDriverWait(driver,10);""wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button" )));"我不知道为什么它在没有等到预期条件的情况下适用于你的,它对我的​​不起作用。
    • 老实说,我仍然感谢您花时间帮助我。正如我所说的那样,总是欢迎提出建议(阅读睡眠线程)。我在这方面太菜鸟了,无法弄清楚为什么它对你有用,而不对我有用。 (可能是您的弹出窗口更快?还是 Eclipse 或 Java 控制台更耐心/更慢?您使用的是哪个版本?可能是那里的互联网连接更快?没有意义。)
    【解决方案2】:

    看起来我正在做与您相同的教程 :) 我遇到了与您完全相同的问题,并且尝试了您所做的几乎所有事情来单击该关闭按钮并在找到此线程之前杀死该弹出窗口。

    问题似乎在于,在我们单击 Find Jobs 后,Selenium 无法立即关闭弹出框。必须设置一个“wait.until..”来等待弹出框出现,这样我们就可以关闭它。这是我所做的:

    package com.indeed.tests;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.WebDriverWait;      //**and this
    
    public class IndeedJobSearch {
    
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
    
        //Create firefox driver to drive the browser
    
        WebDriver driver;
    
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\BURRITOBEAST\\Downloads\\jars\\geckodriver-v0.14.0-win64\\geckodriver.exe");
    
        driver = new FirefoxDriver();
    
        WebDriverWait wait = new WebDriverWait(driver,10); //**and this. 10 is the number of seconds it'll wait before an error is thrown.
    
        //Open Indeed homepage
        driver.get("http://www.indeed.com");
        //Find the 'what' field and enter "selenium"
        driver.findElement(By.id("what")).sendKeys("Selenium");
        //Find the 'location' field and enter "San Diego, CA"
        driver.findElement(By.id("where")).clear();
        driver.findElement(By.id("where")).sendKeys("San Diego, CA");
        //Find the 'findjobs' button and click on it
        driver.findElement(By.id("fj")).click();
    
        wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button"))); //**this is where the magic happens
    
        //Thread.sleep(1000); **tested my idea first using a sleep. then found the wait method after. plus, i want to avoid sleeps if possible to make things speedy.
        driver.findElement(By.id("prime-popover-close-button")).click();
    
        //From the job search results page, get page title and jobs count msg
    
    }
    }
    

    【讨论】:

    • 早上好@BurritoBandit,哇,非常感谢。所以这就是问题所在!该元素无法立即使用,这就是为什么我无法关闭它!!!!谢谢,这让我想到了这些问题,我觉得我学到了一些对未来非常重要的东西!你太棒了!我将阅读有关此预期条件和驾驶员等待的更多信息:)
    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    相关资源
    最近更新 更多