Can't find dynamic Xpath element - xpath

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.

Related

Cypress: How can I find by name

How can I get this input element by name?
<input min="0" class="" name="details["contactEmail"]" type="email" autocomplete="off" value="">
I tried this but it doesn't work
cy.get('[name="details["contactEmail"]"]').type('tara#gmail.com')
also this:
cy.get('[name="details[\"contactEmail\"]"]').type('tara#gmail.com')
I also tried using a lot of combinations on the name attribute by escaping the characters but nothing worked. One suggestion that worked for me was to use the combination of the partial value of the name attribute and the type value. Something like:
cy.get('[name*="details"][type="email"]').type('test')
Or, if you just want to use the name attribute or the type attribute, that works as well.
cy.get('[name*="details"]').type('test')
cy.get('[type="email"]').type('test')

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.

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.

Safari Ajax bug? checked radio not checked

In Safari, If I load a piece of html via XHR (ajax) the browser does not render the radio button that has the checked="checked" attribute as checked.
the html I fetch via ajax:
<input type="radio" name="radiotest" value="off">
<input type="radio" name="radiotest" value="on" checked="checked">
the browser renders two unchecked radio buttons. If I load the exact same code directly from a plain html file (no ajax) the radio buttons render as they should, the last being in its checked state.
what's wrong, is this a known bug? Is there a fix available?
EDIT:
Probing further this looks like a browser bug, and I could not so far fix it with jQuery Post processing, any help appreciated.. here is what I've found:
I have a page that pulls in some form element via ajax.
The html is this:
<input type="checkbox" name="checktest">
<input type="checkbox" name="checktest" checked="checked">
<input type="radio" name="radiotest" value="off">
<input type="radio" name="radiotest" value="on" checked="checked">
after the form element are pulled in, the checkboxed render correct, but the radios both render unckecked. Now, If i run the following jQuery command (from the Safari 5 console):
$('#activemodules input[checked="checked"]');
..it returns an object containing the one checked checkbox.
but if I run the command:
$('#activemodules input[value="on"]');
it actually retuns the correct object, and it even shows its outerHTML property correct like this:
outerHTML: "<input type="radio" name="radiotest" value="on" checked="checked">"
now, if I do:
$('#activemodules input[value="on"]').attr('checked','checked');
Safari get's it, and renders it correctly. I guess I'll just have to pass a data-ischecked="true" attribute and use that to catch and 're-check' all radio buttons after the ajax bit is loaded.. but still, even if I can do this, I'd like to hear any comment or suggestions on this.
This is what my solutions ended like:
First I add a data-ischecked="1" attribute to the radios that are checked:
<input type="radio" name="foo" data-ischecked="1" value="bar" checked="checked">
then, after the ajax call succeeds and finishes I run this:
$('#myradios :radio')
.each(function(){
if( $(this).data('ischecked') ){
$(this).attr('checked','checked');
$(this).removeAttr('data-ischecked');
}
});
This seems to work fine.. but I found out some other stuff of importance:
If I called the data attribute simply data-checked instead of data-ischecked it DID NOT WORK, maybe this is a reserved name?
And also; if I ran the operation on the broader selector scope:
$(':radio')
.each ...
then it also DID NOT WORK
I don't know why, but adding the id of a parent element of the radios that was also part of the html returned by the ajax call to the selecor scope like this:
$('#myradios :radio')
made it work..
I found that mikkelbreum's solution only worked if I did not include the "checked" attribute in the AJAX-injected code.
<input type="radio" name="foo" data-ischecked="1" value="bar">

Resources