Saif Khan
Saif Khan

Reputation: 13

Unable to locate the webelement

I am opening the chrome browser then navigating to the login page of a website. I'm not able to locate the Web Element for the username input field. This is my code:

driver.get("https://petstore.octoperf.com/actions/Account.action");
Thread.sleep(5000);
driver.findElement(By.xpath("//input[@id='stripes-95358526']")).sendKeys("saif");

Upvotes: 1

Views: 62

Answers (3)

DesertPride
DesertPride

Reputation: 133

The id you are using is dynamic so it would change every time you load the page. So either use "name" as others have mentioned or if you want to use id then use following xpath :

By.xpath("//input[starts-with(@id, 'stripes--')"]

Upvotes: 0

frianH
frianH

Reputation: 7563

It's easy with By.name("username"):

driver.findElement(By.name("username")).sendKeys("saif");

And for better perform you can use WebDriverWait instead of Thread.sleep(5000);:

driver.get("https://petstore.octoperf.com/actions/Account.action");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement userName = wait.until(ExpectedConditions.elementToBeClickable(By.name("username")));
userName.sendKeys("saif");

Upvotes: 1

JD308
JD308

Reputation: 146

The Id you are using is dynamic so it isn't going to work as is because it will keep changing. I would use the name of the element like this.

//input[@name='username']

Upvotes: 0

Related Questions