Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver findElement(By.xpath()) not working for me

I've been through the xpath tutorials and checked many other posts, hence I'm not sure what I'm missing. I'm simply trying to find the following element by xpath:

<input class="t-TextBox" type="email" test-id="test-username"/>

I've tried many things, such as:

element = findElement(By.xpath("//[@test-id='test-username']"));

The error is Expression is not a legal expression.

I'm using Firefox on MacBook

Any suggestion would be greatly appreciated.

like image 969
user2457894 Avatar asked Jun 06 '13 01:06

user2457894


4 Answers

element = findElement(By.xpath("//*[@test-id='test-username']"));
element = findElement(By.xpath("//input[@test-id='test-username']"));

(*) - means any tag name.

like image 144
Andrian Durlestean Avatar answered Nov 01 '22 14:11

Andrian Durlestean


Just need to add * at the beginning of xpath and closing bracket at last.

element = findElement(By.xpath("//*[@test-id='test-username']"));
like image 26
Umesh Avatar answered Nov 01 '22 13:11

Umesh


your syntax is completely wrong....you need to give findelement to the driver

i.e your code will be :

WebDriver driver = new FirefoxDriver();
WebeElement element ;

element = driver.findElement(By.xpath("//[@test-id='test-username']"); 

// your xpath is: "//[@test-id='test-username']"

i suggest try this :"//*[@test-id='test-username']"

like image 25
Arpan Buch Avatar answered Nov 01 '22 15:11

Arpan Buch


You missed the closing parenthesis at the end:

element = findElement(By.xpath("//[@test-id='test-username']"));
like image 37
Sitnikov M Avatar answered Nov 01 '22 15:11

Sitnikov M