-1

I'm trying to learn Selenium Webdriver using tutorials online etc...

I'm struggling to overcome this obctacle which is to close this popover.


Using: Laptop: Alienware O.S: Windows 10 64bits Browser: Firefox 51.0.1 (32-bit) Eclipse: Version: 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();
    }

}

`


Expected Result: Selenium Webdriver would open firefox browser, load indeed.co.uk webpage, insert "Selenium" in the first field, insert "London" in the second field, hit the search button, get title and job count values on the console and driver window.

Actual Result: Selenium Webdriver would open firefox browser, load indeed.co.uk webpage, insert "Selenium" in the first field, insert "London" in the second field, hit the search button, STOPS the focus in on the url field and nothing else happens.


I've tried a few solutions but couldn't get it working (https://sqa.stackexchange.com/questions/5310/how-to-close-pop-up-window-in-selenium-webdriver)

e.g.


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();

Note: Not entirely sure my xpath was writen correctly, still learning.

None of these seem to work. I read something about Selenium WebDriver not handling bootstrap popovers, not sure if that's exactly my case, or if any of you has found a solution.

Would love solutions and or advice :)

Thank you very much in Advance.

Community
  • 1
  • 1
Fox23
  • 9
  • 1
  • 1

2 Answers2

0

Your code generally looks fine (other than the use of Thread.Sleep(), which I will address in a minute.

Basically what you want to do in these cases is to right-click on the close X of the dialog and treat it like any other element on the page. Find a locator for the X, in this case it also has an id, prime-popover-close-button, that we can use. All you need to do is grab that element using the id and click it to dismiss the popup. I've simplified the code below.

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();

If you aren't trying to test the UI (entering text and clicking buttons) on the search page, you can just navigate directly to the url and even feed your own keywords in, if you like. See the code below for that.

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();

Now back to Thread.Sleep(). This form of wait is generally a bad practice. You can do some research into the details but suffice it to say that it's not flexible. If you sleep for 10s and the element is present in 25ms, you've waited a long time that you didn't need to. Read up on WebDriverWait and ExpectedConditions. While you didn't need it here, you will eventually need to wait and these are best practices for waiting.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Hello @JeffC, I did try that as I mentioned before on the Question... Tried a few solutions and didn't work. Yogesh Rathi and BurritoBandit had a good point there all I had to do was add a wait for popover to be available and then click. (Didn't cross my mind before... I'm still very new I'll admit). Anyway thank you for the advice on the sleep thread, I will read up more. :) – Fox23 Feb 05 '17 at 11:39
  • The code I posted is working code. I tried it myself and it worked. – JeffC Feb 05 '17 at 21:24
  • Hello @JeffC, Before I posted the question here I had already tried it. I tried a lot of solutions, and they didn't work. Even now, I've created a NewJavaProject to try your code again. On my end, it does not work. BUT if I had the Wait until expected conditions then the rest of the code works. "import org.openqa.selenium.support.ui.WebDriverWait;""WebDriverWait wait = new WebDriverWait(driver,10);""wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button")));" I don't know why it works on yours without the wait until expected conditions, it didn't work on mine. – Fox23 Feb 07 '17 at 12:46
  • To be honest, I still appreciate the time you took to help me out. And an advice is always welcome as I said (read up on sleep thread). I'm too rookie in this to figure out yet why it works on your end and not on mine. (Could it be that the popover is faster on your end? Or is eclipse or the java console more patient/slow? Which version are you using? Could it be internet connection being faster there? Doesn't make sense.) – Fox23 Feb 07 '17 at 12:55
0

It looks like I'm doing the same tutorial you are :) I ran into the exact same issue you did, and tried almost everything you did to click that close button and kill that popover before finding this thread.

It appears that the problem lies in that the popover isn't immediately available for Selenium to close after we click Find Jobs. A 'wait.until..' has to be set in place to wait for the popover to appear so we can close it. Here's what I did:

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

}
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
  • Good morning @BurritoBandit, WOW, thank you very much. SO that's what the issue was! The element was not available immediatly, that's why I couldn't close it!!!! Thank you, it make me think about these issue and I feel I learnt something very important for the future! YOU are Awesome! I'm going to read more about this Expected Conditions and Driver Wait :) – Fox23 Feb 05 '17 at 11:36