0
votes

I'm trying to use selenium webdriver on gmail signup web application to extract the text(month names like january, february...) from Birthday Month field. code is shown as below:

<label id="month-label" class="month">
  <span id="BirthMonth" class=" form-error" aria-invalid="true">
    <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 class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: -299.133px; display: none;" role="listbox" aria-haspopup="true">
        <div id=":1" class="goog-menuitem" role="option" style="-moz-user-select: none;">
          <div class="goog-menuitem-content">January</div>
        </div>
        <div id=":2" class="goog-menuitem" role="option" style="-moz-user-select: none;">
          <div class="goog-menuitem-content">February</div>
        </div>
        <div id=":3" class="goog-menuitem" role="option" style="-moz-user-select: none;">
          <div class="goog-menuitem-content">March</div>
          </div>
        <div id=":4" class="goog-menuitem" role="option" style="-moz-user-select: none;">
          <div class="goog-menuitem-content">April</div>
        </div>
        <div id=":5" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":6" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":7" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":8" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":9" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":a" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":b" class="goog-menuitem" role="option" style="-moz-user-select: none;">
        <div id=":c" class="goog-menuitem" role="option" style="-moz-user-select: none;">
      </div>
    <input id="HiddenBirthMonth" name="BirthMonth" type="hidden">
    </span>
</label>

I tried suing xpath and other element locators but not able to extract the text. When I use the Select class, I'm getting error cannot apply select with div. Please suggest some solution.

1
share your code what you have tried - thebadguy
Tried the following code: Select months = new Select(driver.findbyelement(By.xpath("//div[@class=''goog-menu goog-menu-vertical]/div"))); List <WebElement> months_options = months.getOptions(); for(int i=0;i<months_options.getSize();i++) { System.out.println(months_options.get(i).getText()); } - Afsar Mohammed
You can't use Select() with div elements, use simple click() instead - Andersson
Hi Anderson, My requirement is to get the months text from the nested div tags. Click() is to click on particular month right..? - Afsar Mohammed
click() to open drop-down, click() to select required option... Just like you do it manually - Andersson

1 Answers

0
votes

Here is what you have to do: The month item is a Bootstrap Dropdown. So you have to traverse through it to get the list. Here is the working code:

package Gmail;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class gmailLogin {

    public static void main(String[] args) throws InterruptedException {


        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://gmail.com");
        driver.findElement(By.xpath(".//*[@id='link-signup']/a")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("BirthMonth")).click();
        Thread.sleep(2000);
        List<WebElement> month_menu = driver.findElements(By.xpath(".//div[@class='goog-menuitem-content']"));
        for (int i=0;i<month_menu.size();i++)
        {
            WebElement element = month_menu.get(i);
            String contents = element.getAttribute("innerHTML");
            System.out.println("Values from dropdown is : "+contents);
        }
        driver.quit();

    }

}

Let me know if this helps you.