How to click on a radio button as per the given HTML - java

I am using a reference, shown below, for the radio button.
<input id="checkmo" class="radio" type="radio" data-bind="attr: {'id':getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()" name="payment[method]" value="checkmo"/>
I have used the below code for this but it failed to click on radio button:
WebElement radioBtn = driver.findElement(By.id("checkmo"));
radioBtn.click();

The Radio Button with which you are trying to interact is a React element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.radio#checkmo"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='radio' and #id='checkmo']"))).click();

You may introduce the WebDriverWait which is explicit wait in Selenium.
Code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("checkmo"))).click();

Related

How to click on a radio button using Selenium

I'm trying to click on a radio button in selenium java but it's not clicking.
This is the inspect on the radio button:
<input name="timeSyncType" class="radio" id="radioNTP" type="radio" ng-click="oSettingTimeInfo.szTimeMode='NTP'" ng-checked="oSettingTimeInfo.szTimeMode=='NTP'">
I tried this
WebElement radio = driver.FindElement(By.id("radioNTP"));
radio.click();
also I tried this
WebElement rbutton = driver.findElement(By.xpath("//input[#id='radioNTP']"));
rbutton.click();
but no luck, any help?
Firs you will have to make sure that this id radioNTP is unique in HTMLDOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the //input[#id='radioNTP'] and see, if your desired element is getting highlighted with 1/1 matching node.
If yes, then there are quite a few ways to perform click in Selenium.
Code trial 1:
Thread.sleep(5);
driver.findElement(By.xpath("//input[#id='radioNTP']")).click();
Code trial 2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP']"))).click();
Code trial 3:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
Code trial 4:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
new Actions(driver).moveToElement(button).click().build().perform();
If No, then you will have to change the locator and use the one which represent the desire node.
PS: Thread.sleep(5); is just to solve this problem, if any of the above code works, Please replace the web element with WebDriverWait.
To click on the element you can use either of the following locator strategies:
cssSelector:
driver.findElement(By.cssSelector("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']")).click();
xpath:
driver.findElement(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]")).click();
However, as the element is an Angular element so ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]"))).click();

How to click on button Apply Now

HTML:
Apply Now
I have already tried the following:
driver.findElement(By.xpath("//a[text()='Apply Now']")).click();
driver.findElement(By.xpath("(.//[#href='Apply Now'])")).click();
driver.findElement(By.xpath("//a[#href='Apply Now']")).click();
driver.findElement(By.linkText("Apply Now")).click();
driver.findElement(By.className("Apply Now")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();v
Try to click with webdriver wait till element is properly loaded in Dom and ready to receive click.
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[.='Apply Now']"))).click();
Option 1
Try By.CssSelector
Option 2
Not sure if you are checking if element is exists before clicking on it.if not then use wait.until.
If possible paste error you are getting here.
Hope this helps.
The desired element is a dynamic element so to locate and click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Apply Now"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.btn.btn-success.btn-lg[href*='associateregistration']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='btn btn-success btn-lg' and contains(#href, 'associateregistration')][text()='Apply Now']"))).click();
Try this please, maybe it will help
driver.findElement(By.id("select2-section-tn-container")).click();

Xpath and className are not working even though path and name are correct

I want to click "Action" button and select the option "Update Reference data".
Here is my code
Thread.sleep(30000);// To load the page
driver.findElement(By.xpath("//*[#id=\"spend_mgmt_actions_menu\"]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[id=\"upgradeRefData_button\"]")).click();
I also tried this, but got the same exception
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"spend_mgmt_actions_button\"]")));
driver.findElement(By.className("button show-right-icon f-dropdown-facade actions-list-button plan-action-button")).click();
Thread.sleep(20000);
But when I run the code it gives:
No Such Element Exception unable to locate.....
Below is the HTML code copied from browser by selecting the element.
<button id="spend_mgmt_actions_button"
class="button show-right-icon f-dropdown-facade actions-list-button plan-action-button"
aria-hidden="false" aria-haspopup="true" data-dropdown="spend_mgmt_actions_menu" tabindex="0">
<span>Actions</span><span class="icon icon-right fa fa-caret-down"></span></button>
<button id="upgradeRefData_button" class="list-item dd-item" aria-disabled="false"
data-dropdown-item="" role="menuitem" tabindex="0" title=""><span>Update Reference Data</span>
</button>
You were close. To click() on the element with text as Actions and then to click() the element with text as Update Reference Data you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.button.show-right-icon.f-dropdown-facade.actions-list-button.plan-action-button#spend_mgmt_actions_button>span"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.list-item dd-item#upgradeRefData_button>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button show-right-icon f-dropdown-facade actions-list-button plan-action-button' and #id='spend_mgmt_actions_button']/span[text()='Actions']"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='list-item.dd-item' and #id='upgradeRefData_button']/span[text()='Update Reference Data']"))).click();

Unable to locate dynamic button on dialog box

I try to locate a dynamic button element after click search but it cannot locate it. The Create Account CID sometimes is not clickable and sometime can click.
<div class="pzbtn-rgt" data-click="...">
<div class="pzbtn-mid" data-click="....">
<img src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" alt="" class="pzbtn-i">
Create Individual CID
<img alt="" src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" class="pzbtn-i">
The create individual CID is the one need clicks on.
I using absolute xpath but it still fails. I had tried with many ways. Please help. Thanks.
WebDriverWait waitCIDBtn = new WebDriverWait(driver, 10);
waitCIDBtn.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div")));
WebElement createCID = driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div"));
createCID.click();
As you are trying to invoke click() on the element so, instead of using visibilityOfElementLocated() you need use elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.pzbtn-mid>img.pzbtn-i[src*='zblankimage']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='pzbtn-mid']/img[#class='pzbtn-i' and contains(#src, 'zblankimage')]"))).click();

Getting Selenium "Element not clickable" issue

I am not able to click the element. When I am executing my test in a computer it runs perfectly, but when I am executing my test in a laptop it is failing. Getting error Element not clickable. I have tried to use different wait times as well. Have no idea where is issue. This is what I am using :
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#ng-click='startExam()']"))).click;
Action action=new Actions(driver);
actions.moveToElement(ele).perform();
action.movetoElemet(ele).click().perform.
This what I have in inspect console:
<button class="btn btn-primary ng-scope" ng-if="!proctoredSession" ng-click="startExam()" ng-dissabled="!isExamContentLoaded">Start Exam</button>==$0
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(By.cssSelector("button[ng-click='startExam()']"));
or
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[ng-click='startExam()']")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)",button);
button.click();
As per the HTML within your question the element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solution:
CSS_SELECTOR:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary.ng-scope[ng-click^='startExam']"))).click();
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn-primary ng-scope'][contains(.,'Start Exam')]"))).click();
You may update your existing code as following:
WebDriverWait wait=new WebDriverWait(driver,30);
Action action=new Actions(driver);
WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#ng-click='startExam()']")));
action.moveToElement(temp).click().perform();

Categories