Selenium WebDriver findElement(By.xpath()) not wor

2020-02-11 02:41发布

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.

8条回答
男人必须洒脱
2楼-- · 2020-02-11 03:03

Correct Xpath syntax is like:

//tagname[@value='name']

So you should write something like this:

findElement(By.xpath("//input[@test-id='test-username']"));
查看更多
Melony?
3楼-- · 2020-02-11 03:13

You should add the tag name in the xpath, like:

element = findElement(By.xpath("//input[@test-id='test-username']");
查看更多
唯我独甜
4楼-- · 2020-02-11 03:14
element = findElement(By.xpath("//*[@test-id='test-username']");
element = findElement(By.xpath("//input[@test-id='test-username']");

(*) - any tagname

查看更多
狗以群分
5楼-- · 2020-02-11 03:16

You can use contains too:

element = findElement(By.xpath("//input[contains (@test-id,"test-username")]");
查看更多
干净又极端
6楼-- · 2020-02-11 03:18

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']"

查看更多
地球回转人心会变
7楼-- · 2020-02-11 03:19

You missed the closing parenthesis at the end:

element = findElement(By.xpath("//[@test-id='test-username']"));
查看更多
登录 后发表回答