Reputation: 59
I'm actually just starting to learn Java with Selenium, and I can't receive the current URL. Selenium returns the start URL, not the current one.
It looks like, implicitlywait()
does not work, or did I do something else wrong?
package SeleniumPackage;
import java.util.concurrent.TimeUnit;
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.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class SeleniumClass {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://learn.letskodeit.com/p/practice");
WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();
WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());
driver.findElement(By.linkText("Terms of Use")).click();
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
Upvotes: 3
Views: 4640
Reputation: 193108
As you have invoked click()
in the previous line, next to retrieve the URL you need to induce WebDriverWait in conjunction with ExpectedConditions method urlContains(java.lang.String fraction)
as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("partial_text_of_url"));
System.out.println(driver.getCurrentUrl());
Note : You need to remove the implicitlywait()
because as per the documentation :
Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
Upvotes: 0
Reputation: 4739
You can match with the title if the title is matched then print current URL,it always gives you correct URL
public static void main(String[] args) throws InterruptedException {
System.out.println("Ready to launch the browser");
System.setProperty("webdriver.chrome.driver", "C:/Users/sankalp.gupta/Desktop/JAVASELN/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://learn.letskodeit.com/p/practice");
WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();
WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());
driver.findElement(By.linkText("Terms of Use")).click();
if(driver.getTitle().contains("Practice | Let's Kode It"))
{
System.out.println(driver.getCurrentUrl());
}
}
Upvotes: 0
Reputation: 1987
This is an expected behaviour, since you are clicking on "Terms of Use" of the iframe, which doesn't redirect your URL (just change the iframe's)
I suggest to use xpath to find the desired element like this:
WebElement element = driver.findElement(By.xpath("/html[@class=' video no-videoautoplay']/body/div[@class='view-school']/footer[@class='bottom-menu bottom-menu-inverse']/div[@class='container']/div[@class='row']/div[@class='col-xs-12 col-sm-4 col-md-4 footer-column'][2]/ul[@class='list-unstyled']/li[1]/a"));
Upvotes: 1
Reputation: 2334
You have to add some explicit wait after clicking the "Terms of Use" Link else add some delay.
Modified Code:
driver.findElement(By.linkText("Terms of Use")).click();
//Added Newly
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("Terms"));
String currentURL = driver.getCurrentUrl();
Upvotes: 1