In Firebug and other DevTools you can get the DOM properties and values corresponding to an HTML element.
How can such values be extracted using selenium-java code?
I had tried getAttribute(), but it seems to be working only for HTML attributes and not for DOM properties like "value" or "spellcheck" etc.
The reason I went for this approach is that the value associated with the <input> text field (snippet below) is run-time generated and data is bound to it using Knockout. And hence it's not possible to capture them with standard approaches like getText(), getAttribute("value"), getAttribute("text"), getAttribute("innerHTML"), getAttribute("innertext"), etc.
HTML snippet for the HTML element:
<input class="form-control" type="text" style="cursor: text" readonly="readonly" data-bind="textInput: url">
I know this is an old question but it might give someone else a hand out
Use this in the console
$$("input.form-control").value
if it returns the required you will have to execute the Javascript using WebDriver i.e.
driver.ExecuteScript("var data = arguments[0].value; return data;", (Element as RemoteWebElement)
According to the Selenium documentation, there is only the getAttribute() function, which is described as follows:
Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned (for example for the "value" property of a textarea element). If neither value is set, null is returned. ...
According to this, getAttribute("value") should return the DOM property value in case there is no HTML attribute named value.
If that's not the case, it may be a timing issue. I.e. the value is read by Selenium before it gets set.
In Selenium 4 use getDomAttribute() and getDomProperty().
Related
Part of it look like this:
<input type="hidden" id="recaptcha-token" value="Need This Value">
I'm running Selenium like this:
driver.findElement(By.xpath("[#id=\"recaptcha-token\"]"));
How I can get "Need This Value" from running this code ?
For your specific case, you can first get the element like so:
driver.findElement(By.xpath("//*[#id='recaptcha-token']")); // Any element with that id
driver.findElement(By.xpath("//input[#id='recaptcha-token']")); // More specific to your tag
And then you get the attribute you want using getAttribute(String attrName).
A one-liner would then be:
driver.findElement(By.xpath("//input[#id='recaptcha-token']")).getAttribute("value");
If you only want to look for an element with that id, you could simplify that call by using By.id() instead of By.xpath():
driver.findElement(By.id("recaptcha-token"));
I am trying to find an element with Selenium and Java, the problem is that the element's id, class, and name always increment so I am not able to find it with selenium. Below is what I am currently trying:
WebElement field = driver.findElement(By.xpath("//input[contains(#linkText, 'Broadcast copy')]"));
In my html file these are the attributes that keeps changing:
id="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]"
name="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]"
value="copy (Cluster 102)"
Entire html
<tbody>
<tr class='rowOdd'>
<td><b>Name</b></td>
<td> <input type='text' data-validation='required validate-name-unique validate-name-not-empty' size='65' id='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' name='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' value='copy (Cluster 102)' /> </td>
These always increment and I have no access to the html file to change anything. So my question is how can I find this input element? Thanks in advance.
UPDATE
I get the error:
Unable to locate element:{"method":"id", "selector":"files[.*][.*]"}
I believe the xpath you are using is incorrect. Use
//input[contains(text(), 'Broadcast copy')]
instead of
//input[contains(#linkText, 'Broadcast copy')]
According to the html you have provide the following should work as well
//body[contains(.,'Name')]//input
Try this..
In case "copy (Cluster" text in value attribute is not changing, then you can try below xpath:-
//body[contains(.,'Name')]//input[contains(#value,'copy (Cluster')]
Since the attributes of id, class, and css were constantly changing, 'data-validation' was one that stayed the same all the time. So the code below worked for me.
driver.findElement(By.xpath("//input[#data-validation='required validate-name-unique validate-name-not-empty']"));
I'm using the DOM library for JAVA and some entries XHTML encounter this problem:
[Fatal Error] tree.xml:238:185: Attribute "itemprop" was already specified for element "span".
This is the XHTML part with problems:
<span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person' itemprop='name'>Rodrigo</span>
Exists some option to allow duplicate attributes in DOM?
Thanks!
My understanding is that the Microdata specification only allows one itemprop per HTML element, meaning that the DOM library you're using is properly marking it as invalid markup. If you want to specify multiple values, they need to be space-separated, like this:
<span class='fn' itemprop='author name' itemscope='itemscope' itemtype='http://schema.org/Person'>Rodrigo</span>
Incidentally, the class attribute works the same way.
I am having problems with ValueChangeListener attached to a dropdown list.
Here is the code:
<h:selectOneMenu
value = "#{MultiFileSelectMgmtBean.selectedLocationName}"
valueChangeListener = "#{MultiFileSelectMgmtBean.LocationChangeEvent}"
onchange = "submit();"
>
<f:selectItems
value = "#{MultiFileSelectMgmtBean.locationsListItems}">
</f:selectItems>
</h:selectOneMenu>
And here is the backing bean:
protected List<SelectItem> locationsListItems;
...
public void LocationChangeEvent( ValueChangeEvent vce ) throws Exception
{
selectedLocationName = (String) vce.getNewValue();
}
The problem is that 'selectedLocationName' gets a "11" or "13" value, even the dropdown list is populated with two strings "LocationTest1" and "LocationTest2".
What could be the problem with vce.getNewValue?
The submitted value of the dropdown list is the option value, not the option label as you seem to think. Note that the method is also called getNewValue(), not getNewLabel(). The option labels are not sent over HTTP from client to server by the HTML form submit. There's no way to extract them from the HTTP request.
If you really need the option label instead of the option value for some unclear reason, then you'll either need to use it instead of option value while creating the select items, or to have a mapping of all option labels associated with the option values somewhere, so that you can get the label by the value from this mapping. The chance is big is that you already have this sort of mapping in your bean, otherwise you wouldn't be able to populate the <f:selectItems> value :)
See also:
How to get both label and value from f:selectItems
Our <h:selectOneMenu> tag wiki page
Unrelated to the concrete problem: the combination of a <h:selectOneMenu>, a valueChangeListener and onchange="submit()" indicates that you're using a JSF 1.x specific hack in order to achieve the functional requirement of populating another dropdown or fields based on the change of the dropdown. Since you seem to be already using JSF 2.x, I recommend you to forget about this approach at all and just use <f:ajax listener> instead. The aforelinked wiki page contains one example.
I have a page in my app with a dynamically-generated form, in which I need a number of <select> elements. Since I don't know in advance how many there will be, I need to put an ID number in the name attribute of each <select>. I'm trying to use the built-in #{select} tag (documentation here) like so:
#{ select 'select_' + ${IDnum}}
...options, etc...
#{/select}
When I do that I get a MissingMethodException:
No signature of method: Template_1009.$() is applicable for argument types:
(Template_1009$_run_closure1_closure2_closure3) values:
[Template_1009$_run_closure1_closure2_closure3#ad2388] Possible solutions:
_(java.lang.String), is(java.lang.Object), run(), run(), any(), get(java.lang.String).
When I instead do:
#{ select 'select_${IDnum}'}
the page renders correctly, but the select element renders like this in view-source:
<select name="select_${IDnum}" size="1" >
So, how do I get the value of ${IDnum} into the name attribute? I can do this with normal HTML <select> tags, but I'll need to write some Javascript to emulate Play's value:${x} functionality that I really don't want to bother with.
Thanks!
Try this :
#{select 'select_'+IDNum}