extracting attribute value of parent element using Selenium - java

Experienced with Java, pretty new to Selenium, locators, etc.
Buried deep in some HTML is several similar divisions:
<div tabgroup="topTabs__County Summary" sectiongroup class="field TextDescription tab">
<label for="request_48543">
<span class="label">Monument</span>
</label>
</div>
<div tabgroup="topTabs__County Summary" sectiongroup class="field DropDownList readonly tab">
<label for="request_48543">
<span class="label">Geolocation</span>
</label>
</div>
<div tabgroup="topTabs__County Summary" sectiongroup class="field SingleLineText tab">
<label for="request_48543">
<span class="label">Intersection</span>
</label>
</div
I need some Selenium magic to find a label with a specific value then backtrack to find that label's division and from that division extract the value of a given attribute. Drilling down seems fairly easy but how does one "back up" ?
For example, given "Geolocation" I'd like to extract "field DropDownList readonly tab"
I've tried things like
WebElement chill = m.findElement(By.xpath("../..//span[text='Geolocation']"));
to no avail

You reversed the order of going to the parent element, and you need () in text. The xpath should be
"//span[text()='Geolocation']/../.."
Another option is to look for an element that has a chilled with "Geolocation" text
"//div[.//span[text()='Geolocation']]"
this might give you more results, depends on the html structure that is not in the question. In that case you can add unique attribute, for example tabgroup
"//div[.//span[text()='Geolocation']][#tabgroup]"
this will return only <div> tag that has tabgroup attribute.
To extract the data use getAttribute("class") on chill WebElement

Related

How to enter information in a combo box with look up using Selenium webdriver?

I am having trouble with automating data entry in a combo-box with lookup/auto filling option element. The automation task is filtering data based on a company's name.
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).click();
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).sendKeys(companyname);
((WebElement) driver).sendKeys(Keys.ENTER);
The script runs perfectly up until entering the data.
Relevant HTML:
<div id="div_ff_company_id" class="ff_item filter-field" data-type="filter-field" condition="company_id" operator="is_in" container="multi_select" type="default" data-label="Customer">
<label class="control-label">Customer</label>
<div class="select2-container select2-container-multi input-xlarge filter_item" id="s2id_company_id" style="width:100%"><ul class="select2-choices"> <li class="select2-search-field"> <label for="s2id_autogen79" class="select2-offscreen"></label> <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen79" placeholder="" style="width: 10px;"> </li></ul></div><input type="hidden" id="company_id" style="width:100%" class="input-xlarge filter_item select2-offscreen" tabindex="-1" value="">
Can you guys tell me if there is something I am overlooking?
After clicking on it ... I mean after
driver.findElement(By.xpath(".//*[#id='s2id_company_id']")).click(); statement then use
WebElement currentElement = driver.switchTo().activeElement()// which give the active or currently focused element.
Try #1 : currentElement.sendKeys(companyname);
Try #2 : inspect the element in Dom then use sendkeys
Try #3 : use executeScript() which takes function calls and raw JS, too. You can return a value from it and you can pass lots of complicated arguments to it.
For reference http://www.seleniumhq.org/docs/03_webdriver.jsp#using-javascript
Hope it helps

Extract all visible text from html

I am trying to create a search function in google chrome. Given a string it will highlight all areas containing this string. I use java. I
To do this, first I need to extract all visible text. I have tried to analyze html pages in order to figure out how to extract only text.
For sections that looks like this, it seems
To do this, I planned on using jsoup. I am not sure how to extract text from sections that looks like this. (This is a youtube comment with a "read more" link and "show less" link).
From this section, I try to extract "Not gonna lie, dat dog is ADORABLE" and ("Les mer" or "Vis mindre" depending on which of them is visible).
<div class="comment-renderer-text" tabindex="0" role="article">
<div class="comment-renderer-text-content">Not gonna lie, dat dog is ADORABLE</div>
<div class="comment-text-toggle hid">
<div class="comment-text-toggle-link read-more">
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-link" type="button" onclick="return false;">
<span class="yt-uix-button-content">Les mer
</span>
</button>
</div>
<div class="comment-text-toggle-link show-less hid">
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-link" type="button" onclick="return false;">
<span class="yt-uix-button-content">Vis mindre
</span>
</button>
</div>
</div>
</div>
I am going to assume that the html code given is already in a document named doc.
String text = doc.select("div.comment-renderer-text-content").first().text();
The doc.select command gets Elements that contain that specified HTML query. Then I get the first one and convert it to text.
More can be read here: Jsoup Selector
Edit:
You can use this code to get visible text rather than per class:
String text = doc.body().text();

Java XPath question

I wrote some codes to parse the html using xpath and Java. The html file is something like:
<div class="field_row">
<label for="names">Names *</label>
<input id="address.A" type="text" maxlength="15" size="32" value="12345" name="address.work">
<span class="additional_info"> Information 1 </span>
</div>
<div class="field_row">
<label for="names">Names *</label>
<input id="address.B" type="text" maxlength="15" size="32" value="12345" name="address.work">
<span class="additional_info"> Information 2 </span>
</div>
And Java codes:
public static final Element INFOFIELD= Element.findXPath(".//*[#class='additional_info'");
will let me get 'Information 1'; however, I need to retrieve 'Information 2'. Therefore, I use:
public static final Element INFOFIELD= Element.findXPath(".//*[#class='additional_info' and #id='address.B']");
But got errors. Could you give me some hint please? Thanks. A.
You can create an XPath based on your input field (address.B), and then specify you want to access one of its sibling nodes and thus retrieve its data...
XPath:
//input[#id='address.B']/following-sibling::span[#class='additional_info']
as you can see after we find the input node with the id attribute 'address.b', we specify 'following-sibling'. This indicates that we want to select one of the siblings after the current node('address.B's input field). Then we specify which node that is followed by the attribute details: span[#class='additional_info']
some working code implementing the above XPath:
WebElement element = driver.findElement(By.xpath("//input[#id='address.B']/following-sibling::span[#class='additional_info']"));
System.out.println(element.getText());
will print 'Information 2'
You can use XPath axes in other related ways to access other nodes in the DOM (parents, children, siblings,etc).
http://www.w3schools.com/xpath/xpath_axes.asp
An axis defines a node-set relative to the current node.

How do I determine radio button selection based on value using selenium webdriver?

I am trying to determine which of two radio buttons is selected and based on that select the other one. I'm using Java and selenium.
My HTML is:
<div class="row span-670px">
<h3>Turn on</h3>
<div class="field-row">
<div class="field-wrap radio-row clearfix ">
<input type="radio" name="choosePaymentModel" value="QUOTEHOLD" checked="checked" />
<label>
...
</label>
</div>
</div>
<div class="row last span-670px">
<h3>Turn off</h3>
<div class="field-row">
<div class="field-wrap radio-row clearfix ">
<input type="radio" name="choosePaymentModel" value="BASIC" />
<label>
...
</span>
</label>
</div>
</div>
The only thing that differs is the value attribute. The checked attribute will change based on which one is checked, so the only clear way to differentiate the two is by value. I can't seem to find the proper syntax to grab the correct radio buttons. When utilizing the IDE, the element identifiers swap out with each other depending on the selection so nothing is every unique.
Suggestions?
I had to use:
element = driver.findElement(By.xpath("//input[#name='choosePaymentModel' and #value='QUOTEHOLD']"));
and
element = driver.findElement(By.xpath("//input[#name='choosePaymentModel' and #value='BASIC']"));
to determine which was selected, but unfortunately the click methods did not work on them.
When playing with the IDE was lucky enough to find two separately bizzare elements to click on, which were not in fact elements that contained the "isSelected" values.
In either case, looks like I found the answer to my own problem.
String tempvalue[]=object.split(Concrete.VALUE_SPLIT);
//here I am splitting the values passed through data sheet against radio buttons
String Val_radio =Browser.driver.findElement(By.xpath(OR.getProperty(tempvalue[0])+data+OR.getProperty(tempvalue[1]))).getAttribute("value");
System.out.println(Val_radio);
Boolean radio = Browser.driver.findElement(By.xpath("//input[#name='radio' and #value="+"'"+Val_radio+"'"+"]")).isSelected();
if(radio.booleanValue()==true){
//do something here
}

How to add a component to a label?

I have the following html:
<label wicket:id="drugSearchResult.row.item.label" for="drug_1">[Drug XYZ]
<span wicket:id="drugSearchResult.row.item.info">[Information, Price, Other]</span>
</label>
But label element are not allowed to add a child component.
Is there any way to achieve this html requirement?
This is the designer's requirement:
Drug XYZ // label
Information, Price, Other // span
Make sure you're using FormComponentLabel for the <label> element instead of Label.
Label's purpose is to output text inside the associated element (it can be a <span>, <div> or almost any other tag).
FormComponentLabel's purpose is to model <label> tags. They receive the FormComponent they're related to and automatically output the for attribute with the proper value for the dom id attribute.
Take a look at the Wicket wiki page on Form control labels. They're adding components to FormComponentLabel there.
If you'd like to avoid using FormComponentLabel at all, you shouldn't be giving it a wicket:id attribute, and manually set the DOM id attribute of the element the <label> is going to refer to. Then just use it in the for attribute of the <label>.
For instance:
HTML
<input wicket:id="drug">
<label for="drug_1">[Drug XYZ]
<span wicket:id="drugSearchResult.row.item.info">[Information, Price, Other]</span>
</label>
Java
TextField drug = new TextField("drug");
drug.setMarkupId("drug_1"); // Make sure this ID is unique in the page!
drug.setOutputMarkupId(true);
add(drug);
Label drugDescription = new Label("drugSearchResult.row.item.label", aModel);
add(drugDescription);
Using properties and <wicket:message>
For me, the approach below is useful.
In my project, I have only one location per page where the text for the <label>s and validation messages is defined. It's the properties file of the web page.
The additional <div>s and their class attributes are from Bootstrap.
<div class="form-group required">
<label wicket:for="customer.name1">
<wicket:message key="customer.name1"/>
</label>
<div class="controls">
<input type="text" wicket:id="customer.name1" required class="form-control">
</div>
</div>
Java
add(new RequiredTextField<String>("customer.name1")
.setLabel(new StringResourceModel("customer.name1")));
customerPage.properties
# siehe wicket-core-7.9.0-sources.jar!/org/apache/wicket/Application_de.properties
Required='${label}' ist erforderlich
customer.name1=Name 1
customer.name2=Name 2
customer.department=Abteilung
customer.phone=Telefon
customer.active=aktiv

Categories