Automate : Yahoo Create Account Page , need help to locate radio button Element in Page

Hello Virender ,

I was just trying to automate  : yahoo create account page with selenium webdriver

I am facing an issue when trying to  click on gender radio button

I have googled enough and tried multiple options but with any option the gender radio button is not getting selected .

Pls can u guide further

Below is my code  :

package com.toolsqa.java.basics;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class YahooMailRegistration {

public static void main(String[] args) throws InterruptedException {
String searchString =” yahoo mail”;
String countryCode =”India (+91)”;
WebDriver driver = new FirefoxDriver();
driver.get(“https://google.com”);
// driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.className(“gsfi”)).sendKeys(“yahoo”);
List<WebElement> ulelement = driver.findElements(By.className(“sbqs_c”));
for(WebElement elemnt : ulelement){
System.out.println(searchString + ” searchString”+” element : “+elemnt.getText());
int result = elemnt.getText().compareTo(searchString);
System.out.println(result);
if (result >0){
System.out.println(” element to be clicked : “+elemnt.getText());
elemnt.click();
System.out.println(“after click”);

}
break;
}
WebElement yahoologin = driver.findElement(By.linkText(“Yahoo – login”));
yahoologin.click();
driver.findElement(By.id(“login-signup”)).click();
driver.findElement(By.id(“first-name”)).sendKeys(“nisar”);
driver.findElement(By.id(“last-name”)).sendKeys(“sanadi”);
driver.findElement(By.id(“user-name”)).sendKeys(“nisar.sanadi”);
driver.findElement(By.id(“password”)).sendKeys(“abcdefgh”);
driver.findElement(By.id(“show-password-label”)).click();
driver.findElement(By.className(“country-code-arrow”)).click();;
List< WebElement> contryCode = driver.findElements(By.xpath(“.//a[@role=’menuitem’]”));
for (int i=0;i<contryCode.size();i++)
{
WebElement contryCode1 = contryCode.get(i);
String CountryCode1 = contryCode1.getText();
if (CountryCode1.contains(“+91″))
{
System.out.println(” element to be clicked : “+CountryCode1);
contryCode1.click();
System.out.println(“after click”);
break;
}

}

driver.findElement(By.id(“mobile”)).sendKeys(“1234567890″);

WebElement monthSelector = driver.findElement(By.id(“month”));
Select monthSelect = new Select(monthSelector);
List<WebElement> monthoptions = monthSelect.getOptions();
for ( int j=0;j < monthoptions.size();j++){
System.out.println(” month options : “+monthoptions.get(j).getText());
if(monthoptions.get(j).getText().equalsIgnoreCase(“August”)){
monthoptions.get(j).click();
break;
}
}

WebElement daySelector = driver.findElement(By.id(“day”));
Select daySelect = new Select(daySelector);
List<WebElement> dayoptions = daySelect.getOptions();
for ( int j=0;j < dayoptions.size();j++){
System.out.println(” month options : “+dayoptions.get(j).getText());
if(dayoptions.get(j).getText().equalsIgnoreCase(“22″)){
dayoptions.get(j).click();
break;
}
}

WebElement yearSelector = driver.findElement(By.id(“year”));
Select yearSelect = new Select(yearSelector);
List<WebElement> yearoptions = yearSelect.getOptions();
for ( int j=0;j < yearoptions.size();j++){
System.out.println(” month options : “+yearoptions.get(j).getText());
if(yearoptions.get(j).getText().equalsIgnoreCase(“1985″)){
yearoptions.get(j).click();
break;
}
}

/********************************************** Virender this code is not able to click on the gender radio button ************************************************************************/

List<WebElement> genderoptions = driver.findElements(By.name(“gender”));

System.out.println(“genderoptions : “+genderoptions);
for (int i = 0 ; i< genderoptions.size();i++){
System.out.println(” gender : “+genderoptions.get(i).getAttribute(“value”));
String genderValue = genderoptions.get(i).getAttribute(“value”);
if(genderValue.equalsIgnoreCase(“m”)){

System.out.println(” gender selected “+genderoptions.get(i).isSelected());
genderoptions.get(i).findElement(By.id(“male”)).click();
System.out.println(” gender selected “+genderoptions.get(i).isSelected());
break;
}
}

}
}

4004622 Default Asked on December 8, 2015 in Selenium WebDriver.
Add Comment
1 Answer(s)

try with below code, its working fine
driver.findElement(By.xpath(“/html/body/div[1]/div[2]/form/div/div[6]/fieldset/div/label[2]”)).click();

john Train Answered on March 11, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.