Generic Xpath in selenium - xpath

I'm new to Selenium Webdriver. I have been using Firebug & Firepath to generate xpath (copy pasting the given xpath) for the web elements, but I am facing problems when using such xpaths(Such as "Xpath cannot be evaluated into an web elemnt").
Please help me with the below example of xpath of a Webelement to create a flexible & generic xpath:
<input type="text" maxlength="15" length="40" value="" name="ST_ACK_NUM"/>

Like the people say in the comments it is better to create a more relative path to your elements. Maybe you can post some more input so the XPath can be created more efficiently.
To get the input with a absolute XPath you can do:
//input[#name='ST_ACK_NUM']
Above XPath will search the complete source to all <input> elements where attribute name equals the value ST_ACK_NUM
When you look at your source maybe you can adjust the XPath and add more dependencies. For example if your input looks like:
<div class="DivClass">
<form name="FormName">
<input type="text" maxlength="15" length="40" value="" name="ST_ACK_NUM"/>
</form>
</div>
You could use a XPath like:
//div[#class='DivClass']/form[#name='FormName']/input[#name='ST_ACK_NUM']
This will also find the <input> element, but with a lot more dependencies.

Related

Filling in text box with capybara

I have a text box that I'm trying to fill in with Capybara. I've tried to play around with it and try to figure something's out but my tests don't pass.
Here's
It's for this specific text box:
<span class="ui-grid-header-cell-label ng-binding" ui-grid-one-bind-id-grid="col.uid + '-header-text'" id="14213131-uiGrid-0008-header-text">DOB</span>
<input type="text" class="ui-grid-filter-input ui-grid-filter-input-0 ng-touched" ng-model="colFilter.term" ng-attr-placeholder="{{colFilter.placeholder || ''}}" aria-label="Filter for column" placeholder="" aria-invalid="false" style="">
Here's the code I have.
find('ui-grid-filter-input ui-grid-filter-input-0 ng-touched').set('1414234')
Ideally I'm trying to find this specific text box and type something in.
To fill the <input> using Capybara you can use either of the following locator strategies:
find('[aria-label=Filter for column]').set('1414234')
or
find('input[aria-label=Filter for column]').set('1414234')
As a CSS selector 'ui-grid-filter-input ui-grid-filter-input-0 ng-touched' is looking for a ng-touched element which is a descendant of a ui-grid-filter-input-0 element which is a descendant of a ui-grid-filter-input element - which obviously isn't what you want. Since you're trying to match on classes you need to use the CSS class selector which starts with .
find('.ui-grid-filter-input.ui-grid-filter-input-0.ng-touched')
would be the correct way to do what you were doing, however you probably don't really need all those classes, and the more you specify the more brittle you are making your selectors. It's likely that just
find('.ui-grid-filter-input-0').set('1414234')
would do what you want - or better
find('.ui-grid-filter-input-0').fill_in(with: '1414234')

Can't find dynamic Xpath element

I need your help to find and identify this element:
HTML: <input type="tel" id="pp-DlfVWS-14" autocomplete="off" name="addCreditCardNumber" class="a-input-text a-form-normal">
Where "DlfVWS" change every refresh.
I have tried many combinations with no success and errors
for example:
driver.find_element_by_xpath("//input[contains(#id, 'pp-']")
Thanks.

Xpath Query for <input class="radio" type="radio" name="xyz" value="0">

Could you please let me know the Xpath Query for the html tag:
<input class="radio" type="radio" name="xyz" value="0">
I tried using the one mentioned below but in vain:
xpath = //input[#name='xyz']|//input[#value='0']
Many Thanks!
Zamir
Your XPath expression selects an input with #value='0' iff it is a descendant of an input with #name='xyz' that is anywhere in the document.
So, your expression could match the element you want, but only in that specific context (which I assume it isn't in, as it is not matching).
Your desired element could be matched by any of the following:
//input
//input[#name='xyz']
//input[#value='0']
//input[#name='xyz' and #value='0']
//input[#name='xyz'][#value='0']
//*[#name='xyz'][#value='0']
Which you choose would depend on what it is you don't want to match.
You might also wish to consider not starting with // as that searches the entire document. If you know more about the ancestry of the element in question, you could add that information to achieve a more targeted match.

How to get value from a placeholder using xpath

All of the elements are dynamic. I can see only Placeholder which is unique from the following html:-
<input
id="ext-gen1617"
type="text"
size="20"
class="x-form-field x-form-text x-form-focus"
autocomplete="off"
aria-invalid="false"
placeholder="Gender"
data-errorqtip=""
role="textbox"
aria-describedby="combobox-1166-errorEl"
aria-required="true"
style="width: 78px;"
/>
I need to get the value displayed in
placeholder="Gender".
I tried using
//input[#placeholder='Gender']
But my webdriver script failed to identify it.
Can anyone please help me out with possible solution to it?
String s=driver.findElement(By.xpath("//input[#placeholder='Gender']")).getAttribute("placeholder");
System.out.println(s);
To get an attribute for a filed, you can use the .getAttribute() method.
I assume you are dealing with (front end)script generated web elements, then you must need to embrace lean way of thinking. Don't try to pull out a web element by it's property alone. If you are not getting them try to build a xpath from its parent or siblings.
say, the HTML goes like this,
<div id="somestatic id">
<div id="xyz">
<input name="dynamic one"/>
</div>
</div>
Then you can build a xpath as ,
//*[#id='staticID']/div/input
for the HTML,
<div id="staticID"></div>
<input name="dynamic one"/>
the xpath is ,
//*[#id='staticID']/following-sibling::input
similarly there are n number of option available. Give them a try
Try
driver.findElement(
By.cssSelector("input[id*='ext-gen']")
).getAttribute("placeholder")
Let me know is the above statement is working or not.
The best way is to find the element with CSS selector in this case -
input[placeholder='Gender'], which can easily find the element.

Finding an element by XPath in Selenium

I am trying to use Selenium to navigate a webpage. There is a button I am trying to get to via its xpath. For other buttons on the site, it works fine. But for this particular one, I keep getting the error that the element can't be located. Firebug is just giving me the xpath in this format: //*[#id="continueButton"].
I notice that the button has wrappers around it. They are structured like
<div class = "cButtonWrapper">
<div class = "cButtonHolder">
<input type="image" id="continueButton" name="Continue" alt="Continue" src="/store/images/btn_continue.gif" value="Continue">
</div>
</div>
Could the wrappers around the button have anything to do with not being able to locate it?
Maybe the <input> element cannot be properly located by XPath because you are using invalid HTML. Try using <input id="continueButton"/> or <input id="continueButton"></input> in your page source.

Resources