A field in my app has 2 html codes. When I use OR in its Xpath then it is not detecting the Element. Can anyone help me making an appropriate xpath

84 views Asked by At

A field in my application has two HTML codes. I want to use OR in its XPath so that in any HTML code the command can run. But I am unable to use OR in the xpath. It's not detecting the field when I use OR. Below are both the two HTML code. Can anyone make an XPath using OR so that it runs always? First HTML Code-

 <input autocomplete="off" class="text" maxlength="30" tabindex="2" type="password" id="pswd-input" name="password" onfocus="   pswdLabel.style.display='none';pswdWrap.style.margin='0'    " onblur="if(this.value==''){pswdLabel.style.display='block';pswdWrap.style.margin='0 0 -14px'}else{pswdWrap.style.margin='0'} "> 

and this is Second HTML code-

 <input type="password" name="password" maxlength="35;" autocomplete="off" size="37" value="">==$0

Any help will be appreciated...

2

There are 2 answers

2
Ashish Deshmukh On

Try using Pipe (|) operator

driver.findElement(By.xpath("//input[@id='pswd-input'] | //input[@type='password']"));
1
NotInventedHere On

You were pretty close:

driver.findElement(By.xpath("//input[@id='pswd-input' or @type='password']"));

but why do you need to OR when both versions have type="password" and name="password"? Wouldn't a simple By.name be better? It would match both of these elements:

driver.findElements(By.name("password"));