How to select C++ value if any radio button not selected using selenium - selenium

How to select C++ value if any radio button not selected
<form name="testform" action="" method="POST">
<div align="center">
<br>
<input type="checkbox" name="option-1" value="Java">Java
<input type="checkbox" name="option-2" value="C++">C++
<input type="checkbox" name="option-3" value="Python">Python
</br>
</div>

We can induce a simple check on the WebElement to check if it is selected or not and if not selected click on it to select as follows:
WebElement element= driver.findElement(By.xpath("//input[#name='option-2']"));
if(!element.isSelected())
{
element.click();
}

You can use this :-
webElement element= driver.findElement(By.xpath("//input[#name='option-2']"));
element.getAttribute("value"))
or
element.getText();

Related

Unable to get text from disabled textbox

I need to print value of a disabled text box. The below given is the source code
<div class="col-6 col-md-2 form-group mb-2" xpath="1">
<input name="SMDetailsViewModel.SoRSummaryRowID" class="form-control rum-disabled" id="SRowID" type="hidden" value="3908" tabindex="-1">
<input name="SMDetailsViewModel.ID" **value**="20445" class="form-control rum-disabled " id="SID" type="text" tabindex="-1">
</div>
The 'value' needs to get printed and it is dynamic value.
I have tried with CSS, Xpath like below
WebElement SoR=driver.findElement(By.cssSelector("#SID")); With xpath as well
String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);
tried with GetAttribute as well
Working with below
WebElement SoR=driver.findElement(By.cssSelector("#SID"));
String SoRID=SoR.getAttribute("value");
//String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);

How to iteratively click all elements in a dropdown with selenium?

I am testing a react.js front end web application with selenium and C# automation framework, I need to click all the elements in a drop down list, ideally, I would like to select the drop down as a list of elements, and iterate through each element and click it.
I have tried to locate the dropdown menu By Xpath, Cssselector, cssName, none seems to work, when I debug the code, my "dropDown" variable is always null
Here is the code of the drop down menu
<div class="dropdown-menu shadow px-4 show">
<div>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="1">1 </label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="2">2</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox"value="3">3</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="4">4</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="5">5</label>
<label class="dropdown-item m-0 px-0 d-block"><input type="checkbox" value="6">6</label>
</div>
</div>
here is my selenium code
public static IList<IWebElement> dropDownClick (IWebDriver _driver) {
IList<IWebElement> dropdown = _driver.FindElements (By.ClassName ("dropdown-menu shadow px-4 show"));
return dropdown
}
I expected the variable "dropdown" is not null when I run the code in debug mode
Please use code written below in order to get the elements and to click on each element in iteration:
//Below line Finds the dropdown
WebElement dropdownElement = driver.findElements(By.xpath("//div[contains(#class,'dropdown-menu')]"));
//Below line stores all elements present in dropdown in a list of webelements
List<WebElement> elements = driver.findElements(By.xpath("//div[contains(#class,'dropdown-menu')]//label"));
for(WebElement ele : elements){
//To click on dropdown
dropdownElement.click();
//To click on label present in dropdown. This will change with each Iteration
ele.click();
}
Hope it helps :)
For xpath why not just use:
//div[#class='dropdown-menu shadow px-4 show']//label ---yields 6 rows
If this is not working, make sure the drop down is not in an iframe. You need the //label added so all elements appear in your "FindElements". Without it your return is 1.
Please try with below xpath. May be it is because of sapce,
//div[contains(#class,'dropdown-menu')]//label

how to perform click on an element using selenium for which i am getting StaleElementReferenceException

Problem: there are couple of radio button with same id and I need to perform click on these. The first radio button gets the click where the second one is getting StaleElementReferenceException.
Can Anybody Suggest me how it can be handled so that when I click the second radio button it does not throw StaleElementReferenceException
HTML:
<solvup-radio-type _ngcontent-c7="" _nghost-c10=""><div _ngcontent-c10="" class="validation-error ng-untouched ng-pristine ng-invalid">
<solvup-label _ngcontent-c10="" _nghost-c33=""><label _ngcontent-c33="" class="control-label">
Have you been able to resolve the issue based on this information? <!----><span _ngcontent-c33="" class="required-mark">*</span>
</label>
<solvup-tooltip _ngcontent-c33="" _nghost-c36=""><!---->
</solvup-tooltip>
</solvup-label>
<!----><div _ngcontent-c10="" class="radio">
<label _ngcontent-c10="">
<input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
Yes, issue is resolved - close case
</label>
</div><div _ngcontent-c10="" class="radio">
<label _ngcontent-c10="">
<input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
No - continue to next step
</label>
</div>
</div>
<solvup-hint-text _ngcontent-c10="" _nghost-c34=""><!----><p _ngcontent-c34="" class="help-block"></p>
</solvup-hint-text>
<solvup-validation-messages _ngcontent-c10="" _nghost-c35=""><!---->
<!----><div _ngcontent-c35="" class="form-group">
<small _ngcontent-c35="" class="err"><i _ngcontent-c35="" aria-hidden="true" class="fa fa-exclamation-triangle"></i> Please select an option</small>
</div>
</solvup-validation-messages>
</solvup-radio-type>
Selenium Code:
#Given("^User fills details in First of Five Troubleshooting page$")
public void user_fills_details_in_First_of_Five_Troubleshooting_page() throws Throwable {
Thread.sleep(2000);
List<WebElement> li = driver.findElements(By.className("radio"));
Actions ob = new Actions(driver);
ob.moveToElement(li.get(1));
ob.click(li.get(1));
Action action = ob.build();
action.perform();
}
Please suggest.
Looking at the dom-structure, you might be trying to click an element that might not be the right one. I would think
List<WebElement> li = driver.findElements(By.Id("ts_note_questions"));
Actions ob = new Actions(driver);
ob.moveToElement(li.get(1));
action.perform();
li.get(1).click();
would work, as you are trying to click on the input instead of the div.
I noticed you copied the first radio-button but it looks like you forgot to change the id of the second radio-button. Try to change the id of the second radio-button to a unique one.

Selenium Webdriver- How to click "Log In" button in a webpage

I am new to selenium, tried different methods to click the Login button in a webpage, but ended up with "Unable to locate element: " error. Appreciate your help on how i can do it correctly.
<form class="LoginForm__Form-gmPGuK fIaXrM" autocomplete="off">
<div class="InputWrapper__Wrapper-dYpPUc btujOC">
<label class="Input__Container-evMrUq XWErm"><span class="Input__Label-dTgnUu ikGhjC">Username</span><input class="Input-eXLwAq eqVwiS" name="username" type="text" maxlength="20" autocomplete="off" value="" style="font-size: 1.6rem;"></label>
<p class="InputWrapper__ErrorMessage-bFHpkO jmYdPT">Invalid username</p>
</div>
<div class="Password__Wrap-kEDFHF gajvNg">
<div class="InputWrapper__Wrapper-dYpPUc btujOC"><label class="Input__Container-evMrUq ipuRfT"><span class="Input__Label-dTgnUu ikGhjC">Password</span><input class="Input-eXLwAq eqVwiS" name="password" autocapitalize="off" id="password" type="text" maxlength="20" autocomplete="off" value="" style="font-size: 1.6rem;"></label></div>
</div>
<div class="LoginForm__Agree-djIMue hEHJaH">By logging in, you agree to our <a class="LoginForm__Terms-diivNK iYiAkF" href="https://www.EEE.com/yyyyyyy/rr/terms-and-conditions.html" target="_blank">Terms & Conditions</a></div>
<button class="Button-hYXUXp bTvZVR" type="submit">LOG IN</button>
</form>
Try this code with WebdriverWait
WebElement username= driver.findElement(By.name("username"));
username.sendKeys("dsfs");
WebElement password= driver.findElement(By.name("password"));
password.sendKeys("fsdfdsfs");
WebElement LogInbtn= driver.findElement(By.xpath("//button[text()='LOG IN']"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(LogInbtn));
LogInbtn.click();
If it is in frame then first go to frame and then use above code

Getting value of the element

I need to get the value of data-id in the HTML code below.
My attempted code is:
//p[contains(.,'Smart card')]/following-sibling::button[#data-id='633597015500043521']"));
My HTML code shown below:
<form class="select-card-form" novalidate="" method="post">
<input type="hidden" value="SmartCardSelect_3ca61d51-e601-40de-80fd-308bc47b52c6" name="FormName">
<input type="hidden" value="d79cf158-93ad-4c77-b4bc-516ce8b28302" name="CardId">
<div class="select-item ">
<p>Smart card 1</p>
<button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500043521">
<span>Select</span>
</button>
</div>
</form>
Solution for this (JAVA):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select-item > button"))));
String value = element.getAttribute("data-id");
Java code snippet
WebElement element = driver.findElement(By.xpath("//form[#class='select-card-form']//div[#class='select-item ']//button"));
element.getAttribute("data-id");
Hope this helps.