1

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

5
  • Please elaborate on This code did not work. What exactly is the problem? Commented Mar 11, 2018 at 8:15
  • Add the error stack stacktrace also. This code did not work is a very vague and abstract statement. Commented Mar 11, 2018 at 8:18
  • Now, i have included the error that I am facing right now and also the dynamic xpaths that i have used so far Commented Mar 11, 2018 at 12:40
  • I think the Xpath in your code is the best, better then the alternatives you give. To find out if it's timing issue add a Thread.Sleep(5000) before your first statement in the code block above. When timing is indeed the problem replace the sleep by an explicitwait in your final code. Commented Mar 11, 2018 at 13:21
  • Hi Frank, thanks for you suggestion, tried it but still getting the same error Commented Mar 12, 2018 at 6:11

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Thanks for your suggesstion, i followed as per your comment, upgraded the Selenium version and java version, but still facing the same issue. Please see the error log given below
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='item1520927444953-1']"} (Session info:chrome=65.0.3325.146) (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:10:56.039Z'System info: host: 'VG-IT-LAP-104', ip: '172.20.23.27', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
1

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.

5 Comments

Hi..Thanks for your suggestion, i changed the code as per your comment, but still facing the same issue. any other suggestions please
Are you sure the error message did not change? Because it was specifically referring to the first (0) element in the original. Does it still refer to that same element?
Yes, error message did not change, the same no such element found exception came in the error message.
<li id="item1520927444953-1" class="selected" role="option" style="width: 52px;"> -- <span class="checked" aria-hidden="true"> ✓</span> </li> this is the element i am trying to click
Is it something to do with 'aria-hidden'=true in the <span> tag ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.