how to select element from CSS Dropdown

2,937 views
Skip to first unread message

Gaurang shah

unread,
Oct 5, 2012, 7:20:43 AM10/5/12
to seleniu...@googlegroups.com
Hi Guys, 
Would someone let me know how to select the option from dropdown if that dropdown is created through CSS. 
You can refer www.flipcart.com site for reference 

Following is my code which is not working, it's now showing any error however it's not even selecting Books. 
driver.navigate().to("http://www.flipcart.com");
WebElement searchDropdown = driver.findElement(By.className("fk-menu-selector"));
// searchDropdown.click();
searchDropdown.sendKeys("Books");

Gaurang shah

unread,
Oct 5, 2012, 7:29:49 AM10/5/12
to seleniu...@googlegroups.com
The one way i can do is.. I can execute the javascript like below. But the application i am using doesn't have such javascript function which i can call. 

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("selectMItem('Cameras', 'cameras')");

Is there any way apart form using above JavaScript and first clicking the dropdown and then clicking the option.

Gaurang Shah

RajSekhar

unread,
Oct 5, 2012, 7:52:52 AM10/5/12
to seleniu...@googlegroups.com
Hi Gaurang,

You can you the XPath attribute instead. I wrote in C# 

driver.Navigate().GoToUrl(baseURL);
IWebElement menuSelect = driver.FindElement(By.Id("fk-menuSelIcon"));
IWebElement menuItemBook = driver.FindElement(By.XPath("//div[@id='fk-mI']/ul/li[2]/div"));
menuSelect.Click();
menuITemBook.Click();

You can change the XPath for other items. 

Best regards,
Thota Raj Sekhar.

On Friday, 5 October 2012 16:50:43 UTC+5:30, Gaurang shah wrote:

Gaurang shah

unread,
Oct 5, 2012, 9:37:48 AM10/5/12
to seleniu...@googlegroups.com
Hi Raj Sekhar,

I know this way... But i was thinking if there is another way, more elegance. Coz i have doubt about it's robustness.  

sirus tula

unread,
Oct 5, 2012, 9:47:51 AM10/5/12
to seleniu...@googlegroups.com
Try this, you'll love this.
 

SelectElement mydropdownElement = new SelectElement(driver.FindElement(By.ClassName("fk-menu-selector")));

mydropdownElement.SelectByText(

"Books");

//you can select by either text, value or index.

Hope that helps!

 

Sirus

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/3EYdQ-UzCTsJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
 
- "If you haven't suffered, you haven't lived your life."
 
Thanks,
 
Sirus

Gaurang shah

unread,
Oct 5, 2012, 10:51:13 AM10/5/12
to seleniu...@googlegroups.com
hey sirus,

did you tried this. it gives error as below.
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"

Gaurang Shah

sirus tula

unread,
Oct 5, 2012, 11:05:59 AM10/5/12
to seleniu...@googlegroups.com
That's because youre element is NOT a dropdown but some other element.
 
Paste your html for that element so i can further assist you.
 
Thnks

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/RISQoMVOxLgJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gaurang shah

unread,
Oct 5, 2012, 11:58:12 AM10/5/12
to seleniu...@googlegroups.com
Open the http://flipcart.com site, in that "All Categories" dropdown is what i am talking about.. 

Please let me know if there is any elegant way to deal with it apart form the ways i mention. 


Gaurang Shah

sirus tula

unread,
Oct 5, 2012, 12:37:37 PM10/5/12
to seleniu...@googlegroups.com
Since that is not a select element but a div element many ul elements.
 
Try this,
 
driver.findElement(By.Css("div.line li:nth-of-type(1))).click(); //for 1st element "Books" and so on.
 

Hope that helps.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/6_BprbEJn4kJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Simon Latinwo

unread,
Oct 6, 2012, 5:00:17 AM10/6/12
to seleniu...@googlegroups.com
Hi Sirus, I'm hoping you can help me.

I am trying to automate a web application which user enters only part of the address: house number and post code (zip code), then the rest is automatically
inserted on the next page in a list format (as seen in the screenshot) so users can select the correct one. I have used selectByVisibleText to select the correct address from the generated list and it works but, I find this to be rather inefficient because I have to enter the entire address, and exactly how it is stored for this to work. Is there another method of doing this to check whether the generated address contains just the house number and post code only.
Thanks for your help,

Simon

image.jpeg
Forgive the typos
Sent from my iPhone

sateesh babu

unread,
Oct 8, 2012, 1:16:54 AM10/8/12
to seleniu...@googlegroups.com
HI,
try this,

new Select(driver.findElement(By.className("fk-menu-selector"))).selectByVisibleText("Books");
--
SATEESH BABU.K

image.jpeg

Gaurang shah

unread,
Oct 8, 2012, 3:27:07 AM10/8/12
to seleniu...@googlegroups.com
following code is working fine. 

driver.findElement(By.className("fk-menu-selector")).click();
driver.findElement(By.cssSelector("div#fk-mI li:nth-child(5)")).click(); 

the only problem is, with above code I can select through index only, what if i have to select through visible text ???

Gaurang Shah

RajSekhar

unread,
Oct 9, 2012, 7:26:37 AM10/9/12
to seleniu...@googlegroups.com
Hello Gaurang,

Just wanted to know which version of Selenium are you using?

If it is Selenium RC then the following code will work fine.

selenium.runScript("javascript:selectMItem('Books', 'books')");
selenium.runScript("javascript:selectMItem('Computers', 'computers')");

best regards,
Thota Raj Sekhar.

RajSekhar

unread,
Oct 9, 2012, 7:40:38 AM10/9/12
to seleniu...@googlegroups.com
Hello Gaurang,

Forget my previous reply. I just saw that you were asking about WebDriver. There is a interface in WebDriver called JavascriptExecutor. with the method executeScript, you can achieve the test very easily. As I am working in C# here is the code below. 

driver.Navigate().GoToUrl(baseURL);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("selectMItem('Computers', 'computers')", ""); 

The highlighted item i have taken from the browser. Change the code accordingly for Java. Let me know the results. 

best regards,
Thota Raj Sekhar.

Gaurang shah

unread,
Oct 9, 2012, 8:00:31 AM10/9/12
to seleniu...@googlegroups.com
Hi Rajshkr, 

Please check my first post... i know this method.. but i want another way to do that. coz my actual application doesn't have such function. 

Gaurang

RajSekhar

unread,
Oct 9, 2012, 8:15:33 AM10/9/12
to seleniu...@googlegroups.com
Can you please share the html/css?

Manoj Hans

unread,
Mar 5, 2013, 4:39:17 AM3/5/13
to seleniu...@googlegroups.com
U can select the value from dropdown through visible text using

First Way--
WebElement ddown=driver.findElement(By.id("fk-search-select"));
Dropdown(ddown, "Books, Pens & Stationery");

public static void Dropdown(WebElement element, String value) {
List<WebElement> options = element.findElements(By.tagName("option"));
for (WebElement option : options) {
if(option.getText().equals(value)) {
option.click();
System.out.println("**********Selected value : "+option.getText());
break;
}
}
}

Another way--
 WebElement ddown=driver.findElement(By.id("fk-search-select"));
Dropdown1(ddown, "Home & Kitchen");

public static void Dropdown1(WebElement element, String value) {
new Select(element).selectByVisibleText(value);
}



--Manoj Hans

On Tuesday, March 5, 2013 10:05:19 AM UTC+5:30, ananaya wrote:
Hi Guarang ,

I tried with your code but the error i got was

Compound class names are not supported. Consider searching for one class name and filtering the results or use CSS selectors.

Thanks

On Friday, October 5, 2012 4:50:43 PM UTC+5:30, Gaurang shah wrote:

Arran

unread,
Mar 5, 2013, 4:50:30 AM3/5/13
to seleniu...@googlegroups.com
You will need to post your HTML to get a proper answer. The problem is you've literally copied an answer from the internet and have no idea what it does. 
If you knew what it did you'd be able to solve this on your own.

The error is because you are using a compound CSS selector, that is something like div.classa classb which is what it's complaining about. If you really must select by a compound CSS selector (which is not advised) add in a . in place of spaces in your CSS class names, i.e so it becomes div.classa.classb

On Tuesday, 5 March 2013 04:35:19 UTC, ananaya wrote:
Hi Guarang ,

I tried with your code but the error i got was

Compound class names are not supported. Consider searching for one class name and filtering the results or use CSS selectors.

Thanks

On Friday, October 5, 2012 4:50:43 PM UTC+5:30, Gaurang shah wrote:

David Lai

unread,
Mar 5, 2013, 12:15:14 PM3/5/13
to seleniu...@googlegroups.com
Webdriver I believe has a "Actions"  builder class that can handle chaining a set of lower level interactions.


If that doesn't work, since you're site is using Jquery, you can use the following javascript injection.

(JavaScriptExecutor)webdriver.executeScript('$(".subMenu.camera").show()');

Oscar Rieken

unread,
Mar 6, 2013, 8:12:15 AM3/6/13
to seleniu...@googlegroups.com
I took a look at this and it worked just fine for me when i did this


No xpath, No execute javascript and really nothing too complicated

Thanks
Oscar


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/RGiKQ7IsJdkJ.
Reply all
Reply to author
Forward
0 new messages