4

Im also facing the same error. Unable to select Birthday, Gender & Location in gmail signup page. I'm using Selenium Webdriver.

Please find the below code for birthday.

new Select (driver.findElement(By.id(":0"))).selectByValue("January");

On executing the above code it gives me the below error

> Element should have been "select" but was "div".

Following is the complete code.

public class GmailCreate {
    public static void main(String args[]) throws InterruptedException
    {
        WebDriver driver= new FirefoxDriver(); 
        driver.get("http://gmail.com");
        //By genderDropdown = By.xpath("//*[@id='Gender']/div");
        driver.findElement(By.id("link-signup")).click();
        /*driver.findElement(By.id("FirstName")).sendKeys("ABC");
        driver.findElement(By.id("LastName")).sendKeys("XYZ");
        driver.findElement(By.id("GmailAddress")).sendKeys("abcx0429");
        driver.findElement(By.id("Passwd")).sendKeys("pass1word123");
        driver.findElement(By.id("PasswdAgain")).sendKeys("pass1word123");
        //new Select (driver.findElement(By.id(":0"))).selectByValue("January");
         //Select gender = new Select(driver.findElement(genderDropdown));
           // gender.selectByVisibleText("Male");*/

        //driver.findElement(By.xpath("//span[@id='BirthMonth']/div")).click();
       Thread.sleep(2000);
       // driver.findElement(By.id(":3")).click();


    /*  driver.findElement(By.id("BirthDay")).sendKeys("26");
        driver.findElement(By.id("BirthYear")).sendKeys("1988");
        driver.findElement(By.id("RecoveryPhoneNumber")).sendKeys("9876543210");
        driver.findElement(By.id("RecoveryEmailAddress")).sendKeys("[email protected]");

        if ( !driver.findElement(By.id("SkipCaptcha")).isSelected() )
        {
             driver.findElement(By.id("SkipCaptcha")).click();
             System.out.println("Marked");
        }
        else
        {
             System.out.println("Already Marked");
        }

        if (!driver.findElement(By.id("TermsOfService")).isSelected())
        {
            driver.findElement(By.id("TermsOfService")).click();
        }
        driver.findElement(By.id("submitbutton")).click();*/

        WebElement monthDropdown = driver.findElement(By.xpath("//div[@class='goog-inline-block goog-flat-menu-button jfk-select goog-flat-menu-button-focused goog-flat-menu-button-hover']"));  

     // Select month
     Select month= new Select(monthDropdown);
     month.selectByVisibleText("March");

     // enter birth day       
     WebElement DayTbox = driver.findElement(By.xpath("//input[@id='BirthDay']"));
     DayTbox.clear();
     DayTbox.sendKeys("20");

     // Enter Birth Year 
     WebElement YearTbox = driver.findElement(By.xpath("//input[@id='BirthYear']")); 
     YearTbox.clear();
     YearTbox.sendKeys("2010");


    }
}
2
  • why do you want to GUI automated gmail?
    – Tarun
    Commented Apr 11, 2016 at 7:50
  • I have just started learning automation. so i was doing it for practice. i would appreciate if ppl help me with the issues instead of asking irrelevant questions. Thank You..
    – iAutomate
    Commented Apr 11, 2016 at 11:31

5 Answers 5

4
WebElement monthDropdown = driver.findElement(By.xpath("//div[@class='goog-inline-block goog-flat-menu-button jfk-select goog-flat-menu-button-focused goog-flat-menu-button-hover']"));  

// Select month
Select month= new Select(monthDropdown);
month.selectByVisibleText("March");

// enter birth day       
WebElement DayTbox = driver.findElement(By.xpath("//input[@id='BirthDay']"));
DayTbox.clear();
DayTbox.sendKeys("20");

// Enter Birth Year 
WebElement YearTbox = driver.findElement(By.xpath("//input[@id='BirthYear']")); 
YearTbox.clear();
YearTbox.sendKeys("2010");

Check for Gmail sign up, It works fine...

3
  • Above code is working fine on my PC for Gmail sign up.
    – Bharat Mane
    Commented Apr 19, 2016 at 6:02
  • paste your code here..
    – Bharat Mane
    Commented Apr 19, 2016 at 6:03
  • I have edited my Question and copied the entire code.. Please ignore the commented code..
    – iAutomate
    Commented Apr 21, 2016 at 2:31
3
    // How old are you?
    Select Agefield = new Select(driver.findElement(By.xpath("agefieldXpath")));
    Agefield.selectByVisibleText("26-35");
    Thread.sleep(2000);

    // What is your gender?
    Select gender = new Select(driver.findElement(By.xpath("genderfieldXpath")));
    gender.selectByVisibleText("Male");

Try with this.. definitely it will works.

15
  • is it works iAutomate ?
    – Bharat Mane
    Commented Apr 11, 2016 at 13:39
  • No it did not work. On executing the above code it gives me the below error Element should have been "select" but was "div" Actually it is not a drop down as u can see same thing mentioned by Arun.
    – iAutomate
    Commented Apr 12, 2016 at 3:08
  • 1
    once confirm your 'xpath' or 'id' is correct or not. Is it drop-down list? paste your code here. You may use selectByindex() method....
    – Bharat Mane
    Commented Apr 12, 2016 at 5:37
  • HTML code for the field i am trying to select value from. <span id="BirthMonth"> <div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" aria-activedescendant=":0" title="Birthday"> <div id=":0" class="goog-inline-block goog-flat-menu-button-caption" role="option" aria-setsize="12" aria-posinset="0">Month</div> <div class="goog-inline-block goog-flat-menu-button-dropdown" aria-hidden="true"> </div> </div> <input id="HiddenBirthMonth" type="hidden" name="BirthMonth"> </span>
    – iAutomate
    Commented Apr 12, 2016 at 8:10
  • On facebook the HTML has tag as <Select>. but in Gmail it is <div>. so the same java code will not work for both the fields.
    – iAutomate
    Commented Apr 12, 2016 at 15:01
0

If you look at the page structure i.e. here gmail-Signup page, Birthday, Gender & Location is not more a dropdown with select tag. So if you are using select in WebDriver, you will get the above error. Please try replacing with the below code. It worked for me.

Birthday Field:
WebElement listbox = driver.findElement(By.xpath("//span[@id='BirthMonth']/div"));
listbox.click();

Then do the same for Gender & Location.

Note: Inspecting the element with F12 will give you a clear idea.

1
  • Thanks Arun. Appreciate ur help.. bt the code will only click on the field and show the available Months. but it doesn't select a month to proceed. can u help with that?
    – iAutomate
    Commented Apr 11, 2016 at 11:34
0

well this code will work..

    Actions action = new Actions(driver);

    action.moveToElement(driver.findElement(By.xpath("//*[@id='BirthMonth']/div[1]"))).click().sendKeys(Keys.UP).sendKeys(Keys.ENTER).build().perform();
3
  • we can make it more tuned as the arrow will start from the location that is provided to start from january month we need to focus our driver instance to the january option before using sendkeys to select the right one for us. Commented Nov 15, 2016 at 19:14
  • Actions action = new Actions(driver); driver.findElement(By.xpath("//*[@id='BirthMonth']/div")).click(); Thread.sleep(1000); action.moveToElement(driver.findElement(By.xpath("//*[@id=':1']/div"))).sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).build().perform(); Commented Nov 15, 2016 at 19:14
  • Thanks @swagat i got the same issue and the solution helped me
    – Ritu Nehra
    Commented Nov 16, 2018 at 5:15
0

If this problem still actual. Try this:

driver.FindElement(By.XPath(".//*[@id='BirthMonth']/div[1]")).SendKeys("Feb")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.