How to get value from a placeholder using xpath - 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.

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.

Generic Xpath in selenium

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.

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.

VirtueMart Search Frustrations

I am trying to implement a simple "Virtuemart Only" search in one of the custom modules on a site.
What I'd like is to be able to insert the search tags into an existing table in a mod_custom module. The problem is, I have no idea how to code an <INPUT> tag to work with VM.
Every Google search I've done returns solutions centered on either (1) installing the VM Advanced Search module, (2) using the basic VM Search module, or (3) using Modules Anywhere to load one of the previously mentioned modules into an existing space. Problem is, the site I'm working on doesn't need another module taking up space. It really just needs a simple <INPUT> field in a particular location that sends queries to VM.
Edit: To clarify my purpose, here is a partial of the source code I want to insert the search into:
<table id="product-search" class="product-search">
<tbody>
<tr>
<td></td>
<td><span class="search-title">Order Online</span></td>
</tr>
<tr>
<td><img src="shopping-cart.png" /></td>
<td><**SEARCH CODE HERE**></td>
</tr>
etc... etc...
So you can see, it's just a simple table with some icons, phone numbers, etc., with the search <input> (hopefully) inserted in the middle of it.
I'm guessing I just need to add some stupidly simple code like:
<form action="administrator/components/com_virtuemart/html/shop.search.php" method="get">
Product Search: <input type="text" name="fname" /><input type="submit" value="Submit" />
</form>
Of course, I just get an error trying to directly access that particular script, so I'm wondering: Any VM gurus know how to code this?
You're not going to be able to just insert a tag in to a mod_custom module, that's not how it works. Did you try the VM Advanced Search Module?
http://extensions.joomla.org/extensions/extension-specific/virtuemart-extensions/virtuemart-products-search/8396
This takes the VM Adv search and puts it in a module. It should do what you need.
I managed to figure it out by scouring the page source for sites I know using VM (should have thought of that in the first place). The code is:
<div class="virtuemart_advsearch">
<form name="adv_search" method="post" action="http://www.mywebsite.com">
<input type="hidden" value="shop.browse" name="page"/>
<input type="hidden" value="com_virtuemart" name="option"/>
<input type="hidden" value="9999" name="Itemid"/>
<input type="text" size="20" name="keyword1" class="inputbox"/>
<input type="submit" value="Search" name="search" class="button search"/>
</form>
</div>
The <div> isn't really necessary, but it keeps things neatly divided for now. This is not the cleanest implementation, but it'll do for now.
If you want to search product only in your site then it will be better if you use default product search module in virtue mart. It will work fine.

Resources