how to get the readonly field value using selenium - selenium

I have read only field on Webpage as
<div id="dspIdDescriptionDet-inputEl" class="x-form-display-field" role="input" aria-invalid="false" data-errorqtip="" style="width: 100%;">2010-RR3 XIIIA9 20360726 FLT</div>
When I'm trying to get the display value ("2010-RR3 XIIIA9 20360726 FLT") using getText() or getAttribute("Value") using Webdriver, its fetching nothing.
error on eclipse:
expected:<2010-RR3 XIIIA9 20360726 FLT> but was:<null>
code:
driver.findElement(By.xpath(".//*[#id='dspIdDescriptionDet-i‌​nputEl']")).getAttri‌​bute("value"))

For getting the text of an element which has a unique id try using:
driver.findElement(By.id("dspIdDescriptionDet-inputEl")).getText()

Moving my comment to an answer since it turned out to be the issue...
I would try adding a wait. It may be that the element is present but it takes a bit for the data to populate into the field.

Related

How to send a date value to read-only input boxes using VBA Selenium webdriver

I ran into an issue with a read-only calendar input box. I need to send a date value to it through Selenium VBA. Here is the snippet of the HTML code I'm looking at:
<input class="dxeEditArea dxeEditAreaSys" id="pickupDate_I"
name="ctl00$ctl00$MainPane$Content$MainContent$OPUserControl$pickupDate"
onfocus="ASPx.EGotFocus('pickupDate')" onblur="ASPx.ELostFocus('pickupDate')"
onchange="ASPx.EValueChanged('pickupDate')" type="text" readonly="">
I put a solution I ended up using as a JS code below as a separate answer.
Here is the solution I ended up using: run a JavaScript code to replace the value in the field, rather than using SendKeys. This way we bypass readonly attribute that affects .SendKeys, but using .Value inside the script works:
driver.ExecuteScript ("document.getElementById('pickupDate_I').value = '01/01/2010'")
driver.findElement(By.xpath("//input[#id='pickupDate_I'])).sendKeys("your value");

How to select one from duplicate tag in page in java in selenium webdriver

I am using Selenium WebDriver and I have number of items on a page and each item on page is a separate form type.
I have saved all of these form elements in a list and I am iterating over every item in an attempt to get the name of the element by using the "alt" attribute.
However when I try to get the "name" attribute from the input element it is always returning the first input tag found on that page, not the name attribute of the element I have currently selected.
The syntax I am using is:
((Webdriver imgtags.get(i)).findelement(By.xpath("//input[#name='qty']")).sendKeys ("100");
I have also tried to get the id from the tag by using:
((Webdriver imgtags.get(i)).getAttribute("id");
It's returning a blank value, but it should return the value of the id attribute in that input tag.
I also tried to get the id by using .bytagname but as id is an attribute it is not accessible
Try:
(driver) findElement(By.xpath("//*[contains(local-name(), 'input') and contains(#name, 'qty')]")).sendKeys("100");
To answer the comment by #rrd: to be honest, I have no idea why OP uses ((Webdriver imgtags.get(i)). I don't know what that is. Normally, I just use driver.findElement[...]
Hoping that he knows what works in his framework :D
Selenium Xpath handling is not fully compliant and it does not always treat // as a synonym of descendant-or-self.
Instead try tweaking your code to use the following Xpath:
((Webdriver imgtags.get(i)).findElement(By.xpath("./descendant-or-self::input[#name='qty']")).sendKeys("100");
This will base your search off the currently selected WebElement and then look for any descendants that have a name attribute with a value of "qty".
I would also suggest storing your imgtags array as an array of WebElement e.g.
List<WebElement> imgtags = new ArrayList<>();
This is a much better idea than casting to WebDriver to be able to use .findElement(). This will cause you problems at some point in the future.

How to retrieve the label text as per the html provided?

Sometimes I failed to get the inner text of a web-element; recently working on thePersonal insurance, and failed to get inner text of label (web element). Here are the script and screenshot of webpage inspected:
WebElement detailPostCode =driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
System.out.println("postcode label text "+detailPostCode.getText());
Could any please help me understand the problem. Thank you for your kind concern.
As per the HTML you have shared the <label> with text as Postal code is a child node of the <div> tag with class attribute as q_codePostal.
What went wrong?
As per your code trial you have used:
WebElement detailPostCode = driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
In this expression //label[#for='q_codePostalDetailAU'] will always refer to the descending <input> tag with id attribute as q_codePostalDetailAU. Hence your code trial didn't work.
Solution
As an alternative you can use the following solution:
WebElement detailPostCode = driver.findElement(By.cssSelector("div.q_codePostal>label"));
Can you try this xpath:
driver.findElement(By.xpath("//div[#class='q_codePostal']/label"));

Validating form data that uses a selector with Selenium

I am attempting to automate checking the values of a form. I figured out how to check the values submitted via send_keys, but anything submitted via a selector is giving me trouble. I'm a n00b when it comes to automation.
This works:
driver.find_element_by_xpath("//*[#name='CurrentAddress.LineOne'][#value='123 Something Lane']")
This does not:
driver.find_element_by_xpath("//*[#name='CurrentAddress.StateAbbrev'][#value='TX']")
The code that governs the CurrentAddress.StateAbbrev is
<select class="form-control" data-val="true" data-val-required="* State is required" id="CurrentAddress_StateAbbrev" name="CurrentAddress.StateAbbrev"><option value=""></option>
When I try to validate the State, I get this:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#name='CurrentAddress.StateAbbrev'][#value='TX']"}
The form value stores the input as 'TX' but the page shows the State as 'TEXAS'. I've tried looking for either, but it still cannot locate the element. Attempting to locate it via either #name or #id doesn't change the outcome.
I haven't tested this code, but it should work.
//Rather than creating the object of WebElement create object of Select
Select stateDropdown = new Select(driver.findElement(By.xpath("//select[#class='form-control']"));
//To select by visible text, pass the text
stateDropdown.selectByVisibleText("");
//Get text of first selected option
String selectedValue = stateDropdown.getFirstSelectedOption().getText();
This ended up working for me:
driver.find_element_by_xpath("//*[#class='form-control']/option[text()='TEXAS']")

Selenium preceding-sibling::text() not working

I am having issues with selenium using the next xpath "./preceding-sibling::text()"
I don't understand why, my first thought was that IE wasn't supporting that xpath statement but it didnt work on chrome neither.
What I am trying to do is to verify if a radio button have a certain text "near" it. For example if a radio button is like this
<div>
<label> Yes <input name='radioBtn'></input></label>
<label> No <input name='radioBtn'></input></label>
</div>
This is a simplified scenario where I need to check the "yes" radio button, so what I am doing is to search for the radiobutton and check if it preceding-sibling::text(), but selenium is cant find any element. I know the Xpath works because I test it on chrome developer tools and it returns the text "Yes".
I can't do something like 'input[1]' because I can't be sure that the first one will be always that have the Yes text.
Any idea why isn't this working on selenium? and if there is any work around?
I got to a work around but is kind of specific to the problem. But let's answer the questions 1 at the time.
Any idea why isn't this working on selenium?
It's not working because selenium don't support text elements, so even when selenium find the element it cant map it to a selenium object, i didn't see it because my code hided the selenium exception. The Exception throw is the next one:
An unhandled exception of type
'OpenQA.Selenium.InvalidSelectorException' occurred in WebDriver.dll
Additional information: invalid selector: The result of the xpath
expression "./preceding-sibling::text()" is: [object Text]. It should
be an element
Is there any work around?
Yes it is. What I did was to run a java script using the selenium IJavaScriptExecutor. With the script I revised the preceding sibling text and return it as a string so if the text was equal to the thing I was looking for (i.e Yes) trhat means that is the radio button I was looking for.
The code looks is similar to this (it can have some sintax errors since I didn't copied from the source):
string script = "function GetPrecedingText(node){"+
"var result = document.evaluate('./preceding-sibling::text()[1]', node, null, XPathResult.STRING_TYPE, null);"+
"return (result==null) ? '' : result.stringValue.trim();}"+
"return GetPrecedingText(arguments[0])";
string result = ((driver as IJavaScriptExecutor).ExecuteScript(script, SeleniumElement)).toString();
Hope someone can find this useful :) and thanks for all that tried to help me :)