Selenium WebDriver findElement(By.xpath()) get value of this element - java

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"));

Related

URL changing link

On my HTML-page I have a link for saving recipe, example
addrecipe?id=607691&name=Soft-Bread-Salami-Rolls
It looks like the correct URL when I hold the mouse over the link,
but the browser(?) converts it to something like this
addrecipe?id=607691%26name%3DSoft-Bread-Salami-Rolls
I managed to put atleast id= in the html but I get error when trying to add name= also.
<a th:href="#{'/addrecipe'(id=${recipesinfo.link})}" th:text=Save></a>
Looking at the docs and assuming recipesinfo.link contains your 607691 ID (and nothing else), I think you should be using
<a th:href="#{/addrecipe(id=${recipesinfo.link},name='Soft-Bread-Salami-Rolls')}">Save</a>
If the name value comes from a variable (eg recipesinfo.name), then you would use name=${recipesinfo.name} instead of the string literal.

Element should have been "select" but was "input" how can I resolve this error

My Code:
loginPage.waitHomePage(60,"Order Scheduling");
Assert.assertEquals("Order Scheduling",driver.getTitle());
(new WebDriverWait(driver, 40)).until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_ContentPlaceHolderBody_orderDeliveryControl_lstDelivery_ctrl0_deliveryPanelBar_i0_lstOrderRequestItems_ctrl0_deliveryDetailControl_lstDeliveryLineItem_ctrl0_lineItemControl_lstBuyLineItems_ctrl0_buyLineItemControl_cmbSupplier_Input")));
Assert.assertTrue(driver.findElement(By.id("ctl00_ContentPlaceHolderBody_orderDeliveryControl_lstDelivery_ctrl0_deliveryPanelBar_i0_lstOrderRequestItems_ctrl0_deliveryDetailControl_lstDeliveryLineItem_ctrl0_lineItemControl_lstBuyLineItems_ctrl0_buyLineItemControl_cmbSupplier_Input")).isDisplayed());
Select abcSupplier = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolderBody_orderDeliveryControl_lstDelivery_ctrl0_deliveryPanelBar_i0_lstOrderRequestItems_ctrl0_deliveryDetailControl_lstDeliveryLineItem_ctrl0_lineItemControl_lstBuyLineItems_ctrl0_buyLineItemControl_cmbSupplier_Input")));
/*abcSupplier.selectByIndex(17);*/
abcSupplier.selectByVisibleText("TBD");
}
HTMl Code:
<input
name="ctl00$ContentPlaceHolderBody$orderDeliveryControl$lstDelivery$ctrl0$deliveryPanelBar$i0$lstOrderRequestItems$ctrl0$deliveryDetailControl$lstDeliveryLineItem$ctrl0$lineItemControl$lstBuyLineItems$ctrl0$buyLineItemControl$cmbSupplier"
type="text"
class="rcbInput radPreventDecorate Required_Field_Control Required_Field_Control_Off"
id="ctl00_ContentPlaceHolderBody_orderDeliveryControl_lstDelivery_ctrl0_deliveryPanelBar_i0_lstOrderRequestItems_ctrl0_deliveryDetailControl_lstDeliveryLineItem_ctrl0_lineItemControl_lstBuyLineItems_ctrl0_buyLineItemControl_cmbSupplier_Input"
value="Mansfield Oil Company of Gainesville Inc"
autocomplete="off">
Where did I go wrong?
Examine this line:
Select abcSupplier = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolderBody_orderDeliveryControl_lstDelivery_ctrl0_deliveryPanelBar_i0_lstOrderRequestItems_ctrl0_deliveryDetailControl_lstDeliveryLineItem_ctrl0_lineItemControl_lstBuyLineItems_ctrl0_buyLineItemControl_cmbSupplier_Input")));
You are expecting element of type Select but path that you are sending to function findElement ends by Input. IMHO this clearly hints that something is wrong either in your ID or in expected element type. It is impossible to give you more concrete answer since only you know the exact structure of HTML document you are working with. But you have to provide correct ID and treat element according to its actual type.

Extract DOM property value using Selenium

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().

Select/click on checkbox by labelin Selenium

I have the following html code and would like to select checkbox by label:
<input type="checkbox" onclick="searchResult(this,'8')" id="catalog-8"/>
<label for="catalog-8">
my-assortment </label>
in the above example, by the value "my-assortment"
I tried this: //label[containts('my-assortment')] but it does not work.
More exactly, I want to write something like this:
//input[#type='checkbox'] which has "id" of the value of "for"
in label[contains(., 'my-assortment')]
Does anybody have any idea?
//label[containts('my-assortment')]
First of all, this is not containts - it should be contains (watch the extra t). And, you are not using contains() correctly. It should be:
//label[contains(., 'my-assortment')]
Also, if you want to click the input element by label, preceding-sibling would help here:
//label[contains(., 'my-assortment')]/preceding-sibling::input[#type='checkbox']

Using variables in #{select} tag attributes

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}

Categories