Shantanu Nandan
Shantanu Nandan

Reputation: 1462

Why my test is throwing Exception-Unable to locate element in webdriver?

package testproject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
  public class mytestclass {
    public static void main(String[] args) {
       WebDriver Driver = new FirefoxDriver();
        Driver.get("https://www.gmail.com/");
         WebElement wb= Driver.findElement(By.name("Email"));
         wb.sendKeys("sweta");
         WebElement wb1= Driver.findElement(By.name("Passwd"));
         wb1.sendKeys("123456");
         WebElement wb2= Driver.findElement(By.id("signIn"));
         wb2.click();
         WebElement wb3= Driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"));
         wb3.click();
         WebElement wb4= Driver.findElement(By.id("gb_71"));
         wb4.click();
   }
}

When i am executing this code everything is going fine till the point where i want the sign in button to be clicked. I am getting exception which says that Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} but when i am trying to locate it using fierbug its working fine. In the above mentioned code i changed the email id and password to keep the email safe.

I was facing problem with one more program which i already posted on stakwave so if u can then please have a look at this link-webdriver is not able to click on a hyperlink in firefox

Upvotes: 1

Views: 14075

Answers (5)

user8119294
user8119294

Reputation: 1

//launch browser 
FirefoxDriver driver = new FirefoxDriver(options); 
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
//gmail login :
driver.get("http://www.gmail.com");
driver.findElement(By.id("identifierId")).sendKeys("****",Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.id("password")).sendKeys("***",Keys.ENTER);
//logout:
driver.findElement(By.xpath("//div[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Sign out")).click();

Upvotes: 0

ramkr
ramkr

Reputation: 41

I faced similar problem, issue resolved after setting timeout.

Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

Not sure whats the role of timeout here though.

Upvotes: 1

remove the dot(.) and star(*) from the xpath and give proper tag name in place of star.

for example if @id=gb is the id of div element, place div in place of star. Hope it will work.

Upvotes: 0

Algiz
Algiz

Reputation: 1288

Remove the dot at the beginning of your xpath expression. That way you have an xpath expression thaty could match everything. With the dot at the beginning you might retrict yourself depending on if the current node is the root node or not. Ther eis no way to know it. Just the fact the dot can only give you trouble. Unfortunately you cannot always trust what tools like firebug give you (it is still true in 99% of the case).

Of course, ensure that the elemetns you are targeting are already on the screen as suggested by the previous answer.

Upvotes: 1

Charlie
Charlie

Reputation: 541

Are you certain your page is completely loaded after you sign in?

Did you set a timeout for your webdriver? (how long it has to wait for elements). Probably it reads your html before it's completey loaded.

Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

To find out quickly if this is the problem do Thread.sleep(8000) after you do wb2.click();

Upvotes: 1

Related Questions