Xerox9000
Xerox9000

Reputation: 64

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 :

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.

Thank you !

Upvotes: 0

Views: 104

Answers (3)

Ghanshyam
Ghanshyam

Reputation: 182

please try this one:

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

Upvotes: 0

mkhurmi
mkhurmi

Reputation: 816

Below worked for me:

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

Upvotes: 1

Sahil Paudel
Sahil Paudel

Reputation: 588

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. Below is the corrected code which I have tested.

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

Upvotes: 1

Related Questions