简体   繁体   English

使用 xpath 定位文本字段元素

[英]Locating a text field element using xpath

So i want to locate a text field element using xpath, my code used to look like this, and it worked:所以我想使用 xpath 定位一个文本字段元素,我的代码曾经看起来像这样,它工作:

element = driver.findElement(By.xpath("//input[(@id='login') or (@name = 'login')]"));

But i wanted to make sure that the driver picks a text field, so i changed it to this:但我想确保驱动程序选择一个文本字段,所以我将其更改为:

element = driver.findElement(By.xpath("//input[((@id='login') or (@name = 'login')) and (@type='text']"));

It didn't work, i searched for a solution but i found little to no information on using both OR & AND on xpath.它没有用,我搜索了一个解决方案,但我发现几乎没有关于在 xpath 上同时使用 OR & AND 的信息。

Thank you !谢谢 !

I guess there is a typo on the last @type- it should have been @type= and also there is a missing ) from the same @type condition.我猜最后一个@type 有一个错字——它应该是@type= 并且在相同的@type 条件下也有一个缺失的) Below is the corrected code which I have tested.以下是我测试过的更正代码。

element = driver.findElement(By.xpath("//input[((@id=\"login\") or (@name = \"login\")) and (@type=\"text\")]"));

Below worked for me:下面为我工作:

element = driver.findElement(By.xpath("//input[(@id='login' or @name='login') and @type='text']")

please try this one:请试试这个:

element = driver.findElement(By.xpath("//input[(contains(@id,'login') or contains(@name,'login')) and contains(@type,'text')]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM