Selenium - WebDriver - Java: Why am I getting empty strings from my elements? - java

I have a webpage where some elements get set on page load. I then need to read some of these elements using Selenium. The problem is that when I read them, all I get is an empty string.
This is a partial image of my website:
I'm trying to obtain the Number field.
Here is what the DOM looks like for this element:
<input style="; ; " class="form-control disabled " id="sys_readonly.u_po_coordination.number" value="POI1356285" ng-non-bindable="" readonly="readonly" aria-label="Number">
Here is my JAVA code to get that value:
String num = driver.findElement(By.id("sys_readonly.u_po_coordination.number")).getText();
I've tried using XPath as well. I've also tried getting other elements as well.
The result is ALWAYS the same: a blank string...
What am I missing? The elements exist because I can set various elements, but reading ones that should be set are always blank.
Any help would be greatly appreciated.
Thanks!

driver.findElement(By.id("sys_readonly.u_po_coordination.number")).getAttribute("value");
Found this website which was pretty useful:
https://www.lambdatest.com/blog/how-to-get-text-of-an-element-in-selenium/

Instead of getText() can you try getValue()?

Related

Selenium tooltip text verification

I'm trying to verify the tool tip text using selenium for the following HTML code.But not sure how to proceed further.
HTML CODE:
<div id="divImgAnnualAllowanceType" class="imgHelp" _tooltip="If the client
<br>an arrangement
<br>a durable arrangement
<br>received a Lump Sum.">
</div>
Since the text is separated by <br> I don't know how to retrieve this text.
I tried using the following code but got null value.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("value");
Thanks in Advance :)
<div> tags doesn't (necessarily) has value attribute, use getText() instead
driver.findElement(By.id("divImgAnnualAllowanceType")).getText();
You should specify the attribute which value you need.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
You have to use proper attribute name with getAttribute() method and there is no attribute as value in your targeted <div> element, you can try below code:
String tooltip = driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
System.out.println(tooltip.replace("<br>", ""));
In above code, we are storing the tooltip value in a variable then printing the same after replacing all <br>.
This code is written in Java.

Cannot get input by its value using XPath

I have multiple HTML <input> elements like this:
<input type="text" class="information">
<input type="text" class="information">
<input type="text" class="information">
After entering different texts (e.g. “hello” "hi" "hey") in these input elements and save it I am able to print out their value using element.getAttribute("value"), which gives “hello” "hi" "hey".
However, when I try to grab this input element using XPath
//input[#class='information' and #value='hello']
//input[#class='information' and #value='hi']
//input[#class='information' and #value='hey']
it does not work (can not identify element with the expression). Any idea why this happens or how to get the input element using XPath in this case? Thanks in advance!
Maybe not the best way to do it but
try finding it by Xpath as follows:
xpath=(//input[#type='text'])[2]
the [2] being the boxes number (1/2/3). Once you've found the box you can access its value.
IWebElement box2 = FindElement(By.XPath('//input[#type='text'])[2]'));
box2.getAttribute();
A better way to do this might be trying to select the " :nth-child(n) " of the div. Perhaps google up on that
As nullpointer wrote, you should first get the list of elements using //input[#class='information'] and then have a closer look at each element using getAttribute("value").
You won't be able to find the values via XPath bc they have been entered after loading the page. In order to find the value attributes with XPath, they would have had to be loaded with the page, like in <input type="text" value="hello">, which it isn't in your case.

Select invalid html-tag with Selenium

I am trying to get a WebElement with Selenium:
driver.findElement(By.xpath("//input[#name='j_username']"))
But Selenium says: "Unable to find element with XPath ...".
The XPath is valid, I proofed it with FirePath.
But the input element has the following invalid code:
<input size="10" type="text" name="j_username" maxlength="8">
I can't change the html-file, despite the fact is there any solution to get the webElement?
Thanks in advance!
try select element with css selector. and also verify in firepath(firebug addon that element is located properly).
so your css selector be something like
input[name='j_username']
2nd approach is to use internal firebug mechanism for finding xPaths of elements.
See screen attached below
After these manipulations driver shoulda handle element properly.
Well I will suggest adding an id to your html code -
<input id="j_username"size="10" type="text" name="j_username" maxlength="8">
and findElement by id -
driver.findElement(By.id("j_username"));
I have faced similar issues with xpath(borwser issues??) but id never fails for me. ;)
By the way I feel your code should be -
driver.findElement(By.xpath(".//*[#name='j_username']"));
The best solution is to find out what selenium is doing wrong, but without a URL or sample page to test on it's a little hard. Is there anyway you could dump the HTML into a jsfiddle? If there is do that and paste the url into the question and I'm sure someone can find a solution.
If not however, another way to get the results is to do it with jQuery. If firebug is picking it up but not selenium, then there's no reason why jQuery wouldn't get it. Here's how to go about doing that if needed:
Step 1: Is jQuery already present on the page? If so then you don't need to do this bit, otherwise you will need to add it yourself by using driver.executeScript(addjQueryScript) where the script does something like this.
Step 2: call WebElement input = driver.executeScript(elementSelector); where the elementSelector script would be something like \"return $('input[name=\"j_username\"]')\");
My jQuery's not so good, but I believe that should work...
Best of luck!

get elements from html parser

I'm using JSOUP, and trying to get the elements which start with a particular div tag id. For example:
<div id="test123">.
I need to check if the elements starts with the string "test" and get all the elements.
I looked at http://jsoup.org/cookbook/extracting-data/selector-syntax and I tried a multiple variations using:
doc.select("div:matches(test(*))");
But it still didn't work. Any help would be much appreciated.
Use the attribute-starts-with selector [attr^=value].
Elements elements = doc.select("div[id^=test]");
// ...
This will return all <div> elements with an id attribute starting with test.

HtmlUnit accessing an element without id or Name

How can I access this element:
<input type="submit" value="Save as XML" onclick="some code goes here">
More info: I have to access programmatically a web page and simulate clicking on a button on it, which then will generate a xml file which I hope to be able to save on the local machine.
I am trying to do so by using HtmlUnit libraries, but all examples I could find use getElementById() or getElementByName() methods. Unfortunately, this exact element doesn't have a name or Id, so I failed miserably. I supposed then that the thing I have to do is use the getByXPath() method but I got completely lost into XPath documentation(this matter is all new to me).
I have been stuck on this for a couple of hours so I really need all the help I can get.
Thanks in advance.
There are several options for an XPATH to select that input element.
Below is one option, which looks throughout the document for an input element that has an attribute named type with the value "submit" and an attribute named value with the value "Save as XML".
//input[#type='submit' and #value='Save as XML']
If you could provide a little bit more structure, a more specific (and efficient) XPATH could be created. For instance, something like this might work:
/html/body//form//input[#type='submit' and #value='Save as XML']
You should be able to use the XPATH with code like this:
client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false
page = client.getPage(url)
submitButton = page.getByXPath("/html/body//form//input[#type='submit' and #value='Save as XML']")
Although I would, in most cases, recommend using XPath, if you don't know anything about it you can try the getInputByValue(String value) method. This is an example based on your question:
// Fetch the form somehow
HtmlForm form = this.page.getForms().get(0);
// Get the input by its value
System.out.println(form.getInputByValue("Save as XML").asXml());

Categories

Resources