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

I went through the xpath tutorials and checked many other posts, so I'm not sure what I am missing. I am just trying to find the following element on xpath:

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

I have tried many things, for example:

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

Error Expression is not a legal expression .

I am using Firefox on a MacBook

Any suggestion would be greatly appreciated.

+6
source share
8 answers
 element = findElement(By.xpath("//*[@test-id='test-username']"); element = findElement(By.xpath("//input[@test-id='test-username']"); 

(*) - any tag

+16
source

You must add the tag name in xpath, for example:

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

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

In your code will be:

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

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

I suggest trying the following: "//*[@test-id='test-username']"

+2
source

You missed the closing bracket at the end:

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

You did not specify which html element you are trying to do an absolute xpath search. In your case, this is an input element.

Try the following:

 element = findElement(By.xpath("//input[@class='t-TextBox' and @type='email' and @test- id='test-username']"); 
0
source

The correct Xpath syntax is as follows:

 //tagname[@value='name'] 

So you should write something like this:

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

You just need to add * at the beginning of xpath and the closing parenthesis finally.

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

You can also use also:

 element = findElement(By.xpath("//input[contains (@test-id,"test-username")]"); 
0
source

Source: https://habr.com/ru/post/946634/


All Articles