Rushdi Shams Rushdi Shams - 3 months ago 6x
Java Question

How to select items from drop down menu using Selenium?

I have been trying to automate a search using Selenium. I simply want to search terms (say

Pink Floyd
) but the file type should be pdf. Here is what I have done so far:

//Query term
WebElement element = driver.findElement("as_q"));
String finalQuery = "pink floyd";

//File type selection
WebElement elem = driver.findElement("as_filetype_button"));
elem.sendKeys("Adobe Acrobat pdf (.pdf)");

This puts the term in the appropriate place and the drop down for file types are expanded but pdf option is not selected. Any help?

I am using Selenium 2.53.0.


The following code segment perfectly worked as per the accepted answer for this question. However, all on a sudden the code segment is not working. I am a bit surprised to find this out. Previously, I was able to select PDF automatically with the following code segment but now, nothing gets selected.

WebElement element = driver.findElement("as_q"));
String finalQuery = "pink floyd";

Efx Efx

This is how i do it, find the li that matches the class='goog-menuitem' and value='pdf', i inspected the element. You can go directly with value='pdf' but just to make sure we are looking at the file type dropdown we added the class.


You can still declare it with WebElement, i just prefer it shorthand. Hope this helps.