0

I am trying to automate a test case using java code.I use the selenium server libraries for the same.Now when i reach a certain page i get a drop down box which has a certain number of elements.The drop down box is called 'Genre'. Now what i want to be able to do is to click the drop down box which i want to expand so that at the next step i am able to click on a particular item for eg. Rock/Metal/Pop etc.All the above i am trying to automate but every time i do so i am thrown the same exception :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='Genre']"}

I have tried the various methods available with By ie By.xpath,By.name,By.id etc but to no avail.I am thus copy pasting information related to the 'Genre' box for your reference.Please guide me on which method to use so that i can successfully achieve the goals i just described.

View selection source when i highlight Genre gives me :

<td id="genre" width="50%">
                                                                Genre <br><select name="Genre" id="Genre" class="input_boxbbb" onchange="subgener(this.value)"><option value="0000">All</option><option value="26">AIRTEL JINGLE</option><option value="19">ARTISTS</option><option value="27">BATTERY</option><option value="25">BOLLYWOOD</option><option value="28">
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Phoenix225
  • 167
  • 5
  • 12

2 Answers2

1

Ok so the code Pavel suggested is correct and should work.

i have just one comment about it. instead of iterating over all options manually you can just use one the following:

genres.selectByIndex(int index)
genres.selectByValue(String value) 
genres.selectByVisibleText(String text) 

the problem is with the HTML, notice you have 2 elements with the same id but a different case.

<td id="genre" width="50%">
<select name="Genre" id="Genre">

this may cause problems in curtain browsers and is in general bad practice.

if you control the HTML i would suggest changing the id of the <td> element to something else and use the above code.

if you are not the one writing the the HTML try using

Select genre = new Select(driver.findElementBy(By.name("Genre"))) [Notice the Case sensetivity];

if even that doesn't work try this to inspect all <select> elements in the page and use it to pick the one you need:

List<WebElement> selects = driver.findElementsBy(By.tagName("select"));
for (WebElement select : selects) {
   System.out.println(select)
}

hope it helps

Eli Polonsky
  • 511
  • 1
  • 3
  • 11
0

Assuming you are working with WebDriver. The driver variable later on is considered as valid Webdriver instance:

public void selectGenre(String genreName){ 
   Select genres = new Select(driver.findElement(By.id("genre")));
   List<WebElement> options = new ArrayList<WebElement>;
   options = genres.getOptions();
   for (WebElement genre:options){
       if(genre.getText().equals(genreName)){
           genre.click();
       }
   }
}
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • I get this exception when i try your code : Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "td" – Phoenix225 May 11 '12 at 08:44
  • ok. Try exchange the code to `By.name` – Pavel Janicek May 11 '12 at 09:03
  • Also declaring just Select throws this error message : Cannot instantiate the type Select.It however gets solved when i say :org.openqa.selenium.support.ui.Select genres = new org.openqa.selenium.support.ui.Select(driver.findElement(By.name("genre"))); – Phoenix225 May 11 '12 at 09:06
  • I am just a beginner in Java.So in case my doubt sounds stupid you have my apologies. – Phoenix225 May 11 '12 at 09:08
  • okay ran your code this time using By.name : the exception this time > Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"genre"} Command duration or timeout: 62 milliseconds – Phoenix225 May 11 '12 at 09:08
  • yup. Or you can put this into the import statement: `import org.openqa.selenium.support.ui.Select`. BTW if the by name fails, try putting the Xpath inside the findElement statement. – Pavel Janicek May 11 '12 at 09:10
  • And also another guess - install Selenium IDE and click the select box while recording the test. It will show you how it did locate the element... – Pavel Janicek May 11 '12 at 09:12