Click Radio Button Selenium - java

Been trying to click a radio button on website to no avail.
<df-radio-group class="o-flex o-flex--distribute df-question ng-tns-c33-15 ng-has-value ng-touched ng-dirty ng-valid" aria-label="Title" id="title" aria-labelledby="title" role="radiogroup" style="float: left; width: 100%;">
<!----><df-radio _nghost-c40="" class="ng-tns-c33-15 is-checked" id="md-radio-93fd918"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-93fd918-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-93fd918-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Mr
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-bd05b81"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-bd05b81-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-bd05b81-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Mrs
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ba9f195"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ba9f195-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ba9f195-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Miss
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ec973a5"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ec973a5-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ec973a5-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Ms
</div>
</label></df-radio>
</df-radio-group>
Been trying to click both the radio button and the label but selenium keeps throwing the no such element error and I'm kinda frustrated at this stage.
Might be easier to see on the actual website:
https://www.theaa.ie/car-insurance/journey/getting-started
Its on the page after entering the email. Trying to get some test cases going but these radio buttons dont want to be clicked.

Having a look to the html structure:
You could wait the presence of the web element with id "title" and then find all the elements with class name "md-radio-label-content".
Once you have all of them, you can check the text and click the interested one.
So, for example, if you want to click on "Mr":
WebElement titleRadiogroup = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("title")));
List<WebElement> ele= titleRadiogroup.findElements(By.className("md-radio-label-content"));
for (WebElement el : ele)
{
if(el.getText().equalsIgnoreCase("Mr"))
{
el.click();
}
}

In cases like this, I would use an XPath to click on the element by its contents. One issue you run into here is that these DIVs that contain the titles are full of whitespace (see below).
<div _ngcontent-c40="" class="md-radio-label-content">
Mr
</div>
Because of this, we can't do something simple like
//div[.='Mr']
Another issue is that you can't use contains() because Mr and Mrs both contain the string Mr. We can avoid the whole thing by using normalize-space() which trims whitespace.
//div[normalize-space(.)='Mr']
If it were me, I would wrap this in a function since you are likely to use it repeatedly.
public void selectTitle(String title)
{
driver.findElement(By.xpath("//div[normalize-space(.)='" + title + "']")).click();
}
and call it like
selectTitle("Miss");

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

Clicking span button by id of the parent(with id) - selenium with java

Is it possible to click a span button which is under the div element, also I would like to use the id of the input to make my test stable.
HTML:
<div class="idit-go">
<input id="IDITForm#checkRuleVO" class="GoLong align-left idit-go-text" type="text" value="" title="Validation Rule" tabindex="257" name="unknown" ignoredisableenableaction="true" helptextinfo="" disabled="">
<div class="idit-go-buttons">
<span id="IDITForm#checkRuleVOGoClearButtonClear" class="idit-go-clear" title="Clear" tabindex="259" onclick="clearField('IDITForm#checkRuleVOhidden');clearField('IDITForm#checkRuleVO');;">
<span class="fa-stack idit-go-button" value="" title="" tabindex="258" onclick="newPerformSubAction('selectRule',true);">
<i class="fa fa-stack-1x fa-square"></i>
<i class="fa fa-stack-1x fa-ellipsis-h"></i>
</span>
</div>
How it looks on screen:
As you can see the 3 dots button to the right, that is the button I'd like to click. Thanks in advance.
In this case you could identify the span that has the id attribute and then just use following-sibling (from your example they seems to be on the same level).
driver.findElement(By.xpath(".//[#id='IDITForm#checkRuleVOGoClearButtonClear']/following-sibling::span"));
using css - "span[id='IDITForm#checkRuleVOGoClearButtonClear'] + span[class='fa-stack idit-go-button']"
Okay, no-one got this from what I can see, so I can have a go as well! :)
Here we go Zvika :
element = driver.findElement(By.xpath("//div[#class='idit-go']//span[#class='fa-stack idit-go-button']"));
element.click();
(locating and clicking an element with span id="IDITForm#checkRuleVOGoClearButtonClear")
Best of luck man!:)

selenium web driver finding elements

<div class="ui-dialog-buttonset">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" type="button" role="button" aria-disabled="false">
<span>
<br/>
For use to protect against or prevent actual or potential fraud, unauthorized transactions,claims or other liability.
</span>
<br/>
<br/>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
</div>
<span align="center"> Selection of this option will automatically log you out of the system.</span>
</div>
</div>
I have same button class name for both the buttons . How to find the first button and click it . The only difference in those is span text which i tried but it is not working out .
List<WebElement> buttons=driver.findElements(By.className("ui-dialobuttonset"));
buttons.get(0).click().
dint work
There is a class called ui-state-hover in first button which is not present in button 2. So You can try this xpath:
driver.findElement(By.xpath("//button[contains(#class,'ui-state-hover')]")).click();
The className your finding is of div, to find list of buttons give className of button i.e. ui-button or you can find buttons by tagName
List<WebElement> buttons=driver.findElements(By.className("ui-button"));
buttons.get(0).click();
Try Below code to click on the first button directly..
Make sure to use the correct class name "Class name is wrong in the code you written above"
wd.findElement(By.xpath("//div[#class='ui-dialog-buttonset']/button[1]"))
if it doesnot work try below code
List<WebElement> buttons=wd.findElements(By.xpath("//div[#class='ui-dialog-buttonset']"));
buttons.get(0).click();

How to click Buy Now button using selenium?

Source HTML look like this :
<script id="during-reserve-tpl" type="text/x-lodash-template">
<div class="gd-row">
<div class="gd-col gu16">
<div class="emailModule message module-tmargin">
<div class="error-msg"></div>
<div class="register brdr-btm">
<div class="jbv jbv-orange jbv-buy-big jbv-reserve">Buy Now</div>
</div>
<div class="topTextWrap brdr-btm tmargin20">
<div class="subHeading">
Only one phone per registered user
<p>
First come, first serve!
</p>
</div>
</div>
</div>
</div>
</div>
</script>
When I code : IWebElement buy = driver.FindElement(By.CssSelector(".jbv.jbv-orange.jbv-buy-big.jbv-reserve")); It says Element not found.
I tried putting By.ClassName with while spaces but it says, compound classes are not supported.
Is there any alternative to click it ?
driver.FindElement(By.cssselector("div.jbv.jbv-orange.jbv-buy-big.jbv-reserve"))
In the above example css selector looks for div tag with name and it will look for all the dot with space
Try this By.xpath("//*[contains(#class, 'jbv')]") if it works.
You can try either of these:
IWebElement buy = driver.FindElement(By.CssSelector("div.register>div"));
OR
IWebElement buy = driver.FindElement(By.CssSelector("div.register"));

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
}

Categories