Selenium (Java) - How to click on a link and then test if window is open?

Viewed 2638

I've worked one a project, in which I display a list of names and their corresponding hyperlinks, each hyperlink text is different, so it would look something like this,

  • name 1 (http://somesite.com/name1)
  • name 2 (http://somesite.com/name2)
  • name 3 (http://somesite.com/name3)
  • name 4 (http://somesite.com/name4)
  • Using the following line of code, I am able to get the required element,

    arg1 = "http://somesite.com/name3"
    WebElement urlElement = driver.findElement("//a[contains(text(), '" + arg1 + "')]");
    

    With this, I do get the urlElement. Now when I click on the urlElement,

    urlElement.click();
    

    I was expecting it to open window, so that later down the link, I can call,

    driver.switchTo().window("opened window title")
    

    I was expecting this to work but for some reason it fails stating these is no window by that title.

    3 Answers
    Related