Ripon Al Wasim
Ripon Al Wasim

Reputation: 37756

Writing xpath for selenium tests and difference between them

I have written xpath for "Sign Up" button of facebook as below:

driver.findElement(By.xpath("//*[@id='u_0_7']")).click();
driver.findElement(By.xpath(".//*[@id='u_0_7']")).click();//there is a dot (.) before //

Both of them are working well. What's the difference between two xpath mentioned above?

Upvotes: 2

Views: 476

Answers (1)

Timo Türschmann
Timo Türschmann

Reputation: 4696

// finds all elements matching *[@id='u_0_7'] in the entire document.

.// finds all elements matching *[@id='u_0_7'] in the current context.

In your example, there is no difference, since driver.findElement(By) has global context.

But you can call findElement(By) on a WebElement, for example

driver.findElement(By.xpath("//table")).findElement(By.xpath("//ul"));

would find all <ul>s in the entire document, clearly not intended. But using

driver.findElement(By.xpath("//table")).findElement(By.xpath(".//ul"));

would find all <ul>s that are children of the first found <table>, like it was intended.

Upvotes: 3

Related Questions