Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.7k points)

I'm using Selenium Webdriver in Java. I want to get the current url after clicking the "next" button to move from page 1 to page 2. Here's the code I have:

    WebDriver driver = new FirefoxDriver();

    String startURL = //a starting url;

    String currentURL = null;

    WebDriverWait wait = new WebDriverWait(driver, 10);

    foo(driver,startURL);

    /* go to next page */

    if(driver.findElement(By.xpath("//*[@id='someID']")).isDisplayed()){

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

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='someID']")));

        currentURL = driver.getCurrentUrl();

        System.out.println(currentURL);

    }   

I have both the implicit and explicit wait calls to wait for the page to be fully loaded before I get the current url. However, it's still printing out the url for page 1 (it's expected to be the url for page 2).

1 Answer

0 votes
by (62.9k points)

Like you said since the XPath for the next button is the same on every page it won't work. It's working as coded in that it does wait for the element to be displayed but since it's already displayed then the implicit wait doesn't apply because it doesn't need to wait at all. Why don't you use the fact that the URL changes since from your code it appears to change when the next button is clicked. In Java it would be something like:

WebDriver driver = new FirefoxDriver();

String startURL = //a starting url;

String currentURL = null;

WebDriverWait wait = new WebDriverWait(driver, 10);

foo(driver,startURL);

/* go to next page */

if(driver.findElement(By.xpath("//*[@id='someID']")).isDisplayed()){

    String previousURL = driver.getCurrentUrl();

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

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    ExpectedCondition e = new ExpectedCondition<Boolean>() {

          public Boolean apply(WebDriver d) {

            return (d.getCurrentUrl() != previousURL);

          }

        };

    wait.until(e);

    currentURL = driver.getCurrentUrl();

    System.out.println(currentURL);

Hope this helps! 

Browse Categories

...