Trying to get specific attribute in Selenium Webdriver - selenium

I am tring to get the attribute type="radio" but I'm not sure how to in the selenium webdriver.
I tried by using
if(driver.findElement(By.id("userStatusEnable")).getAttribute("type").equals("radio"))
and also by changing the id to x-auto-210
<div id="userStatusEnable" class="x-form-check-wrap x-form-field x-component " role="presentation" style="position: relative;">
<input id="x-auto-210" class=" x-form-radio" type="radio" name="gxt.RadioGroup.5" style="position: relative; left: 0px; top: 4px;" tabindex="0" value="enabled" aria-describedby="x-auto-190" checked="">
<label class="x-form-cb-label" for="x-auto-210" htmlfor="x-auto-210" style="position: relative; left: 1px; top: 3px;">Enable</label>
</div>

One possible approach is to use findElements() and an xpath selector to find input tags with a type="radio":
if(driver.findElements(By.xpath("//input[#type='radio']")).size() == 2)

From your question it sounds like you want to find all input elements with the id x-auto-210 and the type radio. You can do that with the following XPath:
"//input[#id='x-auto-210' and #type='radio']"
I've added an explanation of what the XPath expression is doing
The // says we want to search all element
The input means we are only interested in input elements
The [] contains the conditions we want the input to match (namely, that the id is x-auto-210 and the type is radio)
If you use this expression in conjunction with findElements in selenium, you should be able to find the desired elements
if (driver.findElements(By.XPath("//input[#id='x-auto-210' and #type='radio']")).size() == 2) {
//Do stuff
}

Related

How to fetch id from <div> tag using xpath in efficient way

Tried many approach to get the id but I'm not able to find exact Id or efficient result as there multiple ids and its sharing multiple same class.
Tried following approaches but I'm not getting the exact Id needed
//Approach 1
divId = driver.findElement(By.xpath("(//div[#data-activity-type='CompatCheck'])")).getAttribute("id");
//Approach 2
divId = driver.findElement(By.xpath("(//div[#data-activity-type='CompatCheck'])[2]")).getAttribute("id");
//Approach 3
driver.findElement(By.xpath("//div[#data-activity-type='CompatCheck'] and contains(.,'AndroidTesting')]")).getAttribute("id"));
Please find the HTML Code below :
<div>
<strong>Reena</strong>:
<label id="compatcheck_label">
<em class="Highlight" style="padding: 1px; box-shadow: rgb(229, 229, 229) 1px 1px; border-radius: 3px;
background-color: rgb(0, 191, 255); color: rgb(0, 0, 0); font-style: inherit;" match="test" loopnumber="472046515">Test</em>device testing - AndroidTesting
</label>
</div>
<div>
<span style="float: none;" class="special-apps-list">(Special Tag)
</span>
<label id="label_1100"></label>
<span style="float: none;">0d 4h 39m 49s</span>
</div>
<div id="div_1100" data-activity-type="CompatCheck" class="mainActivity">
</div>
From the above HTML code observed that label id and div id is same (1100) , and what differs from other class is "device testing - AndroidTesting"
Is there any approach we can get either label id or div id (as both are same) by using "device testing - AndroidTesting" as it is the only unique and constant element.
Expected output using "device testing - AndroidTesting" as unique
label_1100
div_1100
As per the given HTML to print the desired texts you can use the following following locator strategies:
Printing label_1100:
System.out.println(driver.findElement(By.xpath("//label[#id='compatcheck_label'][contains(., 'AndroidTesting')]//following::div//label[1]")).getAttribute("id"));
Printing div_1100:
System.out.println(driver.findElement(By.xpath("//label[#id='compatcheck_label'][contains(., 'AndroidTesting')]//following::div[2]")).getAttribute("id"));

Correct Syntax for this Selenium command

I am just not sure what will be the syntax for this
This is a drop-down which changes its ID so I wish to choose the label above to navigate the click
Here is the HTML
<label class="form-title">Charging and Billing Alignment</label>
<select name="ratingAndBillingAlignment" class="reverse-toggle-select check-if-form-row-visible hide-child-if-not-visible loadable-drop-down-37 loadable-drop-down-select" id="ratingAndBillingAlignment" makeloadabledropdown="37" style="display: none;" fixed_position=".popup-content-inner">
<option value="TRUE" class="done-into-select-dd">Yes</option>
<option value="FALSE" selected="selected" class="done-into-select-dd">No</option>
</select><span num="37" class="loadable-drop-down loadable-drop-down-container loadable-drop-down-container-37" id="makeLoadableDropDown37" style="position: relative;" title="No" title2="No"><span class="dropdown-label initialized" style="width: 100%;"><span class="dropdown-html"><span class="value_box" style="display: none;">FALSE</span><span class="text">No</span></span></span><span style="display: none; position: absolute; width: 100%; left: 0px;" class="dropdown-list custom-drop-down-dropdown-list custom-drop-down-dropdown-list-37" stop="0" loading="0" data-counter="37"><div class="option-list" style="max-height: 250px;"><div class="dropdown-link the-dd-counter-1 first-dd-link" c="1" title="Yes"><span class="dropdown-html"><span class="value_box" style="display: none;">TRUE</span><span class="text">Yes</span></span></div><div class="dropdown-link the-dd-counter-2 last-dd-link selected" c="2" title="No"><span class="dropdown-html"><span class="value_box" style="display: none;">FALSE</span><span class="text">No</span></span></div></div></span></span>
driver.findElement(By.xpath(
"//span[#class='dropdown-label initialized']/following-sibling::label[contains(text(),'Charging and Billing Alignment')and(#xpath='1')]"))
.click();
Thanks for your help!
Here is the xpath that you can use with name. As name does not update as it did in the case of id.
//select[#name='ratingAndBillingAlignment']
Locating <select> won't work in your case as its hidden. style="display: none;"
Try using below xpath to click on span looks alike dropdown
//label[contains(.,'Charging and Billing Alignment')]/following-sibling::span[contains(#class,'loadable-drop-down')]

Capture list from autocomplete drop down

I am trying to capture list from this drop down.
<input aria-autocomplete="list" aria-owns="typeaheadDataPlain" autocomplete="off" class="clear-btn-input gcw-storeable text gcw-origin gcw-required gcw-distinct-locations " data-gcw-field-type="origin" data-gcw-storeable-name="gcw-origin" data-js-theme="autocomplete" data-lob="FLIGHTS" data-locale="en_US" data-mask="95" data-minchar="1" data-provide="utypeahead" data-template="hbs/templates/destinationCustom" data-tooltip-id="typeaheadDataPlain" data-version="v4" data-airport_code_element="flight-origin-hp-flight-airport_code" data-hotel_id_element="flight-origin-hp-flight-hotel_id" data-lat_long_element="flight-origin-hp-flight-lat_long" data-region_id_element="flight-origin-hp-flight-region_id" data-region_type_element="flight-origin-hp-flight-region_type" data-station_code_element="flight-origin-hp-flight-station_code" data-city_element="flight-origin-hp-flight-city" data-country_code_element="flight-origin-hp-flight-country_code" data-category_element="flight-origin-hp-flight-category" data-heds_element="flight-origin-hp-flight-heds" data-search_name_element="flight-origin-hp-flight-search_name" data-ta_element="flight-origin-hp-flight-ta" id="flight-origin-hp-flight" placeholder="City or airport" spellcheck="false" type="text">
but I can't find no "ul" and I don't know where it's getting the list from. This is what I see when the list appears in HTML.
<div class="autocomplete-dropdown" style="left: -2px; max-width: 604px; min-width: 286px; width: 604px; display: none;"></div>

Selenium VBA select option input element

I'm using Excel VBA, Selenium and Chrome.
Threse is a dropdownbox box and I'm trying to select an option. Html code is:
<div class="x-form-field-wrap x-form-field-trigger-wrap x-abs-layout-item x-trigger-wrap-focus" id="ext-gen437" style="width: 100px; left: 330px; top: 70px;">
<input maxlength="1" spellcheck="false" id="ext-comp-1233" name="CITTADINANZA" class="x-form-text x-form-field x-field-uppercase x-trigger-noedit x-form-focus" readonly="" style="width: 75px;" />
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt="" class="x-form-trigger x-form-arrow-trigger" id="ext-gen438" />
</div>
The dispalyed text options are "Si" and "No" but in Html code there are no options, index, value...(it's just an input element; the img element is the arrow you have to click for a dropdown)
I tried to fill the input element using:
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByIndex
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByValue
driver.FindElementByCss("#ext-comp-1233").AsSelect.SelectByText
driver.FindElementByCss("#ext-comp-1233").Sendkeys
but I recive the error:
"Unexpected TagName Error.
Expected=select
Got=input"
...while Sendkeys doesn't work.
Do you have any idea?
Thanks :)
Try this:
obj.FindElementByXPath("xpath of element").AsSelect.SelectByText ("option text")
Note:
driver - name of the object you've created for webdriver.
The solution is to click on the drop down image, and when the dynamically created divs are displayed, click on those divs.

Selenium Webdriver - (Java) - Click a button with dynamic ID

1) I have a dialog on my web page having 2 buttons, Yes & No.
2) IDs of these buttons are dynamicaly changing every time.
3) How to handle this situation and click on Yes button?
4) Both buttons, Yes & No, have same classname 1.e. rwInnerSpan
5) Here is the Xpath for Yes button
(.//*[#id='confirm1381468352443_content']/div/div[2]/a[1]/span/span)
the part 1381468352443 in xpath is dynamically changing.
Below is the source code of page
`
<tr class="rwTitleRow">
<tr class="rwContentRow">
<td class="rwCorner rwBodyLeft"> </td>
<td class="rwWindowContent" valign="top">
<iframe frameborder="0" name="confirm1381468352443" src="javascript:'<html></html>';" style="width: 100%; height: 100%; border: 0px none; display: none;" tabindex="0">
<div id="confirm1381468352443_content">
<div class="rwDialogPopup">
<div class="rwDialogText">
<div>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(true);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">Yes</span>
</span>
</a>
<a class="rwPopupButton" href="javascript:void(0);" onclick="$find('confirm1381468352443').close(false);" tabindex="-1">
<span class="rwOuterSpan">
<span class="rwInnerSpan">No</span>
</span>
</a>`
Thanks in Advance !!
You can directly check for the text in your Xpath:
driver.findElements(By.xpath("//a[#class='rwPopupButton']/span/span[contains(text(), 'Yes')]"))
There is a way to locate objects using partial link text, so you can try this:
driver.findElement(By.partialLinkText("Yes")).click();
Plain By.linkText may not work because of additional spaces or characters in the link.
You can click on the button based on the Text. Following method will give you a webelement based on the class locator and text.
WebElement getElementBasedOnClassAndText(String classLocator, String text){
List<WebElement> elements = driver.findElements(By.className(classLocator))
for(WebElement element : elements){
if(element.getText().contentEquals(text)){
return element
}
}
Assert.fail("Unable to find any element with the said Text")
}
Once you get the element you can take any action on it.
Since it is dynamic I would look for the changed name:
//Find the dynamicly created ID
String dynamicID = driver.findElement(By
.xpath("//iframe[contains(#name,'confirm')]")
.getAttribute("name");
//Use that ID to find the Yes option
driver.findElement(By
.xpath("//*[#id='"+dynamicID +"_content']/div/div[2]/a[1]/span/span")
.click();