Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium get current url after loading a page

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).

like image 982
pew007 Avatar asked Apr 26 '13 17:04

pew007


People also ask

How do you get the current page URL in Selenium?

Click on the “console” tab. In the console, type “document. URL” command and hit enter. You will give the current URL.

How does Selenium validate current URL?

New Selenium IDE We can obtain the URL of the current page with Selenium webdriver. This is achieved with the help of getCurrentUrl() method. It fetches the URL of the opened application. This method accepts no parameters and strings the URL in the form of String.

What is the use of getPageSource ()?

getPageSource() is method of WebDriver class. So driver. getPageSource() returns source code of the page which stored as string. contains is method of a String class to check if a string contains in another string.


1 Answers

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. I do C# but I guess 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); }  
like image 87
ragamufin Avatar answered Sep 29 '22 06:09

ragamufin