Unable to select text from dropdown - Gmail.com Month Dropdown

1,122 views
Skip to first unread message

Selenium User

unread,
Apr 1, 2012, 8:13:59 PM4/1/12
to webd...@googlegroups.com
Trying to select a month from a drop down in www.gmail.com
Able to identify element and perform a click evnet, but it is not
selecting a month from the drop down.


HTML Snippet from Firebug.
<div class="goog-inline-block goog-flat-menu-button jfk-select"
title="" role="listbox" style="-moz-user-select: none;" tabindex="0"
aria-haspopup="false" aria-expanded="false" aria-activedescendant="">
       <div class="goog-inline-block goog-flat-menu-button-caption">Month</
div>
       <div class="goog-inline-block goog-flat-menu-button-dropdown">&nbsp;</
div>
</div>
<input id="HiddenBirthMonth" type="hidden" name="BirthMonth"
value="09">
<div class="goog-menu goog-menu-vertical" style="-moz-user-select:
none; visibility: visible; left: 0px; top: -194.917px; display: none;"
role="menu" aria-haspopup="true" aria-activedescendant="">
       <div id=":0" class="goog-menuitem" role="option" style="-moz-user-
select: none;">
               <div class="goog-menuitem-content">January</div>
       </div>
       <div id=":1" class="goog-menuitem" role="option" style="-moz-user-
select: none;">
               <div class="goog-menuitem-content">February</div>
       </div>
       <div id=":2" class="goog-menuitem" role="option" style="-moz-user-
select: none;">
               <div class="goog-menuitem-content">March</div>
       </div>
</div>

on mouse over on the div it is changing the style and below div is
getting visible and listing the dropdown.


Java Code.
// XPath - //div[@class='goog-inline-block goog-flat-menu-button jfk-
select']
boolean boolMonthXPathExists = false;
//String strMonthXPathElementId = "//div[@class='goog-inline-block
goog-flat-menu-button jfk-select']";
String strMonthXPathElementId = "//div[@class='goog-inline-block goog-
flat-menu-button jfk-select']";
String strMonthXPathIdentifier = "xpath";
boolMonthXPathExists = checkElement(driver,
strMonthXPathElementId,strMonthXPathIdentifier);
if(boolMonthXPathExists) {
       driver.findElement(By.xpath(strMonthXPathElementId)).click();
}

// Month - //div[@id=':1']
boolean boolMonthListExists = false;
String strMonthListElementId = "//div[@id=':1']";
String strMonthListIdentifier = "xpath";
boolMonthListExists = checkElement(driver,
strMonthListElementId,strMonthListIdentifier);
if(boolMonthListExists) {
       driver.findElement(By.cssSelector(strMonthListElementId));
       System.out.println("Element Identified boolMonthListExists --> " +
boolMonthListExists);
}

// Month - //div[text()='January']
boolean boolMonthListTextExists = false;
String strMonthListTextElementId = "//div[text()='January']";
String strMonthListTextIdentifier = "xpath";
boolMonthListTextExists = checkElement(driver,
strMonthListTextElementId,strMonthListTextIdentifier);
if(boolMonthListTextExists) {
       driver.findElement(By.cssSelector(strMonthListTextElementId));
       System.out.println("Element Identified strMonthListTextElementId -->
" + strMonthListTextElementId) ;
}


private static boolean checkElement(WebDriver driver, String
strElementId, String strIdentifier) {
       boolean boolExists = false;
       while(true) {
               driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
               driver.manage().timeouts().implicitlyWait(10,
TimeUnit.MILLISECONDS);

               if("id".equalsIgnoreCase(strIdentifier)) {
                       boolExists = driver.findElement(By.id(strElementId)).isDisplayed();
                       System.out.println("Element identified in id --> " + boolExists);
               } else if ("cssSelector".equalsIgnoreCase(strIdentifier)) {
                       boolExists =
driver.findElement(By.cssSelector(strElementId)).isDisplayed();
                       System.out.println("Element identified in cssSelector --> " +
boolExists);
               } else if ("xpath".equalsIgnoreCase(strIdentifier)) {
                       boolExists =
driver.findElement(By.xpath(strElementId)).isDisplayed();
                       System.out.println("Element identified in xpath --> " +
boolExists);
               }
               if(boolExists) {
                       break;
               }
       }
       return boolExists;
}


Console Error.

Caused by: org.openqa.selenium.remote.ErrorHandler
$UnknownServerException: The given selector //div[text()='January'] is
either invalid or does not result in a WebElement. The following error
occurred:

Please help to perform a click on the dropdown.

Thanks

Starter. 

Siva

unread,
Mar 19, 2015, 10:32:01 AM3/19/15
to webd...@googlegroups.com

Hi,

Im also facing the same error. Unable to select Birthday, Gender & Location in gmail signup page.
Im using Java + Webdriver.

Please find the below code for birthday.
 

WebElement select = d.findElement(By.xpath("//*[@id='BirthMonth']/div"));
((JavascriptExecutor)d).executeScript("arguments[0].style.opacity = 100;", select);
(new Select(select)).selectByVisibleText("April");

On executing the above code it gives me the below error
Element should have been "select" but was "div"

Kindly help.

Reards,
Siva

darrell

unread,
Mar 20, 2015, 10:32:11 AM3/20/15
to webd...@googlegroups.com
I always try to avoid using JavascriptExecutor. This feels like you are coupling the test to the implementation. If you ever want to refactor the code it could break your test. One idea behind test automation is that I can refactor my code and use the unchanged test automation to confirm the test still passes.

To select the month on a Gmail account creation I would do the following:

- select the link to create a new account
- click the Month selector to make the list of months appear
- wait for the month I want to select to be visible
- select the month

The code in Java would be:

        // go to www.gmail.com
        driver.get("http://www.gmail.com");

        // select the link to create a new account
        driver.findElement(By.cssSelector("#link-signup")).click();

        // click the Month selector to make the list of months appear
        WebElement monthSelector = driver.findElement(By.cssSelector("#BirthMonth>div[title='Birthday']"));
        driver.findElement(By.cssSelector("#BirthMonth>div[role='listbox']>div[id=':0']")).click();

        // wait for the month I want to select to be visible
        By monthLocator = By.xpath("//div[text()='December']");
        wdw.until(ExpectedConditions.visibilityOfElementLocated(monthLocator));

        // select the month
        driver.findElement(monthLocator).click();

Notice that I broke the test down in comments. Once I had all the steps clearly listed it was a simple matter of converting the English comments to Selenium calls. I can do a similar thing with selecting the gender and location. I would actually code it as:

        selectMonth("December");
        selectGender("Male");
        selectLocation("Canada");

then put the code to do each in methods.

SuperKevy

unread,
Mar 20, 2015, 10:32:21 AM3/20/15
to webd...@googlegroups.com
Why are you testing GMAIL for SignUp.  Are you working for google?
This appears to be a violation of google's permitted and licensed uses.
This may be an ethical issue that is also a violation of this groups usage permissions.
Reply all
Reply to author
Forward
0 new messages