How to check radio button for gender selection in selenium webdriver? - java

I want to click on radio button, if radio button is already selected then it should skip for the selection.
For more details on this visit this site, go to the Signed in option -> Create an account, then you will be able to getting the page, that is provided to the screenshot.
Refer Image:

Please Find the workable code:
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
Thread.sleep(2000);
driver.findElement(By.xpath(".//a[#class='login']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//input[#id='email_create']")).sendKeys("abc123_1#yahoo.com");
Thread.sleep(1000);
driver.findElement(By.xpath(".//button[#id='SubmitCreate']")).click();
Thread.sleep(4000);
driver.findElement(By.xpath(".//input[#id='id_gender1']")).click();
Thread.sleep(1000);
You can put implicit waits as well

Try this way to check that the MR radio button is selected or not? If MR radio button is not selected then only if condition will execute.
WebElement MR = driver.findElement(By.xpath("//div/span/input[#id='id_gender1']"));
if(!MR.isSelected())
{
MR.click();
}
OR
If you want to check that the Mrs radio button is selected or not? If Mrs radio button is not selected then only if condition will execute.
WebElement Mrs = driver.findElement(By.xpath("//div/span/input[#id='id_gender2']"));
if(!Mrs.isSelected())
{
Mrs.click();
}

Related

How to enable button do verify in Selenium WebDriver?

I have one button section product, but it disables when hover mouse on section product then the button is enabled. I don't know write script do enable a button when Selenium WebDriver finds it.
#Test(priority = 4)
public void TestSelectedItem() {
driver.findElement(By.xpath("/html/body/app-root/ng-component/div/product-list/div/div/div/div/div[2]/div/div[3]/div/div[1]/a")).click();
Select drpItem = new Select(driver.findElement(By.id("pa_colors")));
drpItem.selectByIndex(2);
driver.findElement(By.className("add_to_cart_button")).click();
}
Please help me.
I will give you code in Python which do hover action on the element:
hover_company = driver.find_element_by_xpath("your XPath")
driver.execute_script("arguments[0].scrollIntoView(true);", hover_company)
hover = ActionChains(driver).move_to_element(hover_company)
hover.perform()
First two lines scroll to your element, last two lines -- do hover action on the element.
After this action -- you can click on the button. Try on.
Code in Java:
hover_company = driver.findElement(By.xpath("your XPath"));
driver.executeScript("arguments[0].scrollIntoView(true);", hover_company);
Actions hover = new Actions(driver);
hover.moveToElement(hover_company).perform();

Selenium Java, How to check if multiple radio buttons are selected?

I am working on a framework and have tried the following and not results :
List<WebElement> rows = EarningNormal.oRdioList;
java.util.Iterator<WebElement> i = rows.iterator();
while(i.hasNext()) {
WebElement ContribIDYES = i.next();
//System.out.println(sitcodes.getText());
if(ContribIDYES.isSelected()){
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES","Contribution ID's are set to YES" , "PASS", "Done");
}
else{
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES", "Contribution ID's are NOT seto to YES as default", "FAILED",TestDriver.comUtil.getImageFileLoc(TestDriver.globalProps.getWebDriver()));
}
}
Hi when you want to verify if a radio button is selected or not then please pay attention that any input tag with type radio has a hidden attribute value known as selected if a radio button is selected then its value is = True and if not then its value is = null,hence on the basis of this you can easily identify which radio button is selcted or not .below find a working example for the same
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("C:\\Users\\rajnish\\Desktop\\myradio.html");
// working with radio buttons
// first take all the radio buttons inside a list like below
List<WebElement> myradioloc = driver.findElements(By.xpath("//*[#type='radio']"));
// apply the for loop to identify/ verify if a radio button is selected or not
for(int i=0;i<myradioloc.size();i++){
System.out.println("attribut value Selected of radio button is : " + myradioloc.get(i).getAttribute("selected"));
// if radio button is selected then value of selected attribute is True else null
if( myradioloc.get(i).getAttribute("selected").equals("null")){
// as if loop will only run when value of selected attribute is null
// i.e only when radio button is not selected
myradioloc.get(i).click();
}
}

Find checkbox and add text

I would like to add in the secondary text field at this website a text string.
My selenium method looks like the following:
public void addSecondaryText(String string) {
//click secondary button
WebElement secButton = driver.findElement(By.xpath("//label/input[#id='sub-text-check']"));
if (secButton.isDisplayed()) {
secButton.click();
}
//clear text
WebElement secTextField = driver.findElement(By.xpath("//*[#id='sub-text']"));
secTextField.clear(); // I get the exception here!
//add text
secTextField.sendKeys(string);
}
However, I currently get an exception for secTextField.clear();, that it cannot be manipulated:
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated
Any suggestions what I am doing wrong?
I appreciate your replies!
The issue here is that the textarea depends on another button and you need to perform a click on it before making the textarea editable. See the image below.
//List will help of to determine the "Add secondary text" button needs to be clicked on not
//if the count is greater than 0 then click it
IList<IWebElement> elements = driver.FindElements(By.XPath("//span[contains(text(),'Add secondary text')]"));
if (elements.Count>0)
{
elements.FirstOrDefault().Click();
}
IWebElement element = driver.FindElement(By.Id("sub-text"));
element.Clear();
element.SendKeys("Test");
Following code should work -
public void addSecondaryText(String string) {
//click secondary button
WebElement secButton = driver.findElement(By.xpath("//span[#class='sub-text-toggle']"));
secButton.click();
//clear text
WebElement secTextField = driver.findElement(By.xpath("//*[#id='sub-text']"));
secTextField.clear(); // I get the exception here!
//add text
secTextField.sendKeys(string);
}

How To Perform Multiple Radio Button Click in Selenium Webdriver

How to make condition to perform multiple radio button click (see image below)...?
multiple radio button click
I've tried for hours, but I got only single radio button click (see attached image)
single radio button click
List<WebElement> radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println("Total element is " + radiobutton.size());
for (int i = 0; i < radiobutton.size(); i++) {
// if you are getting stale element exception because of the page
// reload
radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println(radiobutton.get(i).getAttribute("value"));
// select your radio and click to go to next page
radiobutton.get(i).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#id='btnCheckFare']"))
.click();
Thread.sleep(3000);
}
I saw your video.You have multiple groups of radio button and you can select one radio button in each group.
You have to use 2 loops,one for group and one inner loop for select radio button in a group.
If you have experience in programming then you can do it easily.Otherwise provide the link where we can see this functionality.

Unable to click a mouse hover link using Selenium WebDriver with Java

I am trying to click a mouse hover link using the code below. The webdriver (v.2.35) doesn't throw any error but the element isn't clicked. Can somebody help me figure out what's wrong?
String URL = "http://www.kgisliim.ac.in/"
String menu ="Alumni>Register"
driver.get(URL);
String[] menuItems = menu.split(">");
Actions actions = new Actions(driver);
WebElement tempElem;
for (int i =0 ; i< menuItems.length ; i++) {
tempElem = driver.findElement(By.linkText(menuItems[i].trim()));
actions.moveToElement(tempElem).build().perform();
}
actions.click();
actions.perform();
NOTE: The above code works fine in the below scenario
String URL = "http://www.flipkart.com/"
String menu ="Clothing>Jeans"
You can try this:
WebDriver driver=new FirefoxDriver();
driver.get("http://www.kgisliim.ac.in/");
Actions actions=new Actions(driver);
WebElement menuHoverLink=driver.findElement(By.linkText("Alumni"));
actions.moveToElement(menuHoverLink);
//driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement subLink=driver.findElement(By.cssSelector(".options>ul>li>a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Since the menu on http://www.kgisliim.ac.in/ takes a second to slide out, you could add a WebDriverWait to make sure the submenu has time to become visible before moving the cursor to it. Try replacing the first line in your for loop with the following line. This will wait a maximum of 5 seconds for the submenu (but will return the WebElement as fast as possible within that time).
tempElem = new WebDriverWait(driver, 5).until(ExpectedConditions
.elementToBeClickable(By.linkText(menuItems[i].trim())));
I stumbled across a similar issue recently, with phantomJS and ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).
You can check the window size with
driver.manage().window().getSize()
And you can change it with
driver.manage().window().setSize(new Dimension(width, height));

Categories