driver.findElement – What does it do

Hello guys,

 

I have the following  code:

1
2
3
4
5
6
7
8
9
10
11
12
13
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebDriverCommands_2 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(“http://demoqa.com/frames-and-windows/”);
driver.findElement(By.xpath(“.//*[@id=’tabs-1′]/div/p/a”)).click();
driver.close();
}
}

 

Basically they say that the command driver.findElement(By.xpath(“.//*[@id=’tabs-1′]/div/p/a”)).click(); is used to click on a New Window button.

First of all I don’t understand exactly what that means, second of all,  if I run the script with or without that command I get the same result.

 

Thank you! 🙂

 

Add Comment
  • 1 Answer(s)

     driver.findElement(By.xpath(“.//*[@id=’tabs-1′]/div/p/a”)).click() this code finds New Browser Tab  link and not New window button. To know how it works you should read more on selenium FindElement methods. To explain above it first finds anything with ID = tabs-1 then finds next div tab element in that tree then next p tag element and then a tag element. In this example it works like tree structure. First get parent node then get next node and so on. 

    RE: driver.findElement - What does it doXpath represented by path in question is highlighted in this image.

    as the URL “http://demoqa.com/frames-and-windows/” and target of link represented by above xpath are same it looks like it doesn’t do anything but it loads same page in new tab because target of the link being clicked is same as loaded page.

     

    Default Answered on June 26, 2018.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.