samuel durairaj
samuel durairaj

Reputation: 11

Automating Bootstrap dropdown using Selenium java

Below is the code of bootstrap drop-down. After selecting any value from the drop down the tick mark '✓' is also getting selected.

<ul style="max-height:48px;margin:0px 0px 0px 0px;" aria-expanded="false" aria-activedescendant="item1520752761987-1" aria-label="Select your size" title="Select your size" role="listbox" tabindex="0">
    <li id="item1520752761987-1" class="selected" role="option" style="width: 52px;">
    --
    <span class="checked" aria-hidden="true"> ✓</span>
    </li>
    <li id="item1520752761987-2" role="option" data-value="248" style="width: 52px;">7</li>
    <li id="item1520752761987-3" role="option" data-value="304" style="width: 52px;">8</li>
    <li id="item1520752761987-4" role="option" data-value="306" style="width: 52px;">9</li>
    <li id="item1520752761987-5" role="option" data-value="278" style="width: 52px;">10</li>
    <li id="item1520752761987-6" role="option" data-value="280" style="width: 52px;">11</li>
    <li id="item1520752761987-7" role="option" data-value="282" style="width: 52px;">12</li>
    <li id="item1520752761987-8" role="option" data-value="284" style="width: 52px;">13</li>
    <li id="item1520752761987-9" role="option" data-value="285" style="width: 52px;">14</li>
    <li id="item1520752761987-10" role="option" data-value="286" style="width: 52px;">15</li>
    </ul>

Unable to select values from the drop down. Below, is the selenium Java code that i used.

List<WebElement> dd_menu=driver.findElements(By.xpath("//ul[@title='Select your size']//li"));
    for(int i=0;i<dd_menu.size();i++)
    {
        WebElement element=dd_menu.get(i);
        String val=element.getAttribute("innerHTML");
        if(val.contentEquals("11"))
        {
            element.click();
        }
        //System.out.println("Values From Dropdown : "+val);
    }

    driver.findElement(By.xpath(".//*[@id='product-addtocart-button']")).click();
}

The above selenium java code for clicking bootstrap dropdown did not work...i need help

Getting the error message in eclipse given below:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[@class='checked']//parent::li[@id='item1520579060429-1']"} Command duration or timeout: 48 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35' System info: host: 'VG-IT-LAP-104', ip: '192.168.1.100', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79' *** Element info: {Using=xpath, value=//span[@class='checked']//parent::li[@id='item1520579060429-1']} Session ID: 2ca11707-7e86-4811-a76b-b7461d3dfb92 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=33.0.3}]

Tried different combination of dynamic xpaths given below, but still did not work:

//li[contains(@id,'item1520491614103-8')]//following::span[@class='checked']
//li[contains(.,'13')]
//span[@class='checked']//parent::li[@id='item1520579060429-1']

Can you please guide me where it went wrong..if my xpath is incorrect to select any value from the drop down..please advise with a correct xpath

Upvotes: 1

Views: 996

Answers (2)

Bill Hileman
Bill Hileman

Reputation: 2836

Change the line:

for(int i=0;i<dd_menu.size();i++)

to

for(int i=1;i<dd_menu.size();i++)

Based on your error message, it is attempting to access the first list item which is actually a hidden entry (the check mark). By starting your loop at zero instead of one, you are including that list item. If the rest of your code is correct, skipping that item should work, or at least produce a different error.

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193208

As per your code you have used the following Locator Strategy :

driver.findElements(By.xpath("//ul[@title='Select your size']//li"));

But you are seeing an error as :

org.openqa.selenium.NoSuchElementException: Unable to locate element

I don't see any error as such in your code block however your main issue is the version compatibility between the binaries you are using as follows :

  • Your Selenium Client version is 2.45.0 of 2015-03-05 22:01:35 which is 3 years older.
  • Your JDK version is 1.7.0_79 which is ancient.

So there is a clear mismatch between the current JDK version (8u162) and the current Selenium Client version (v10.0.0)

Solution

  • Upgrade ChromeDriver to JDK 8u162 level.
  • Upgrade Selenium Client to 10.0.0 level.
  • Clean your Project Workspace and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
  • If your base Web Browser version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of the Web Browser.
  • Execute your @Test.

Upvotes: 1

Related Questions