As this is dropdown, you should select values by using one of the below options:
1) selectByVisibleText
2) selectByValue
3) selectByIndex
Try with below code:
WebElement dropdown=d.findElement(By.xpath("//form[@class='form-inline']/div[2]/div[@class='second_input']/select[@class='form-control']"));
Select s = new Select(dropdown);
s.selectByVisibleText("Egypt");
Agreed. But this is the second approach. Why isn't it working? What is wrong in my approach. This approach is explained in the module too?
Also, can you pls tell me what is wrong in this piece of code
WebElement dropList=driver.findElement(By.name("country_id"));
List<WebElement> options=dropList.findElements(By.tagName("option"));
System.out.println("Total number of options are: "+options.size());
Why the options.size() is giving me zero.
(Note:- I know how to do it using Select class. But I am not able to find the mistake)
Xpath you are using is incomplete one. try with either one of the below xpath.
WebElement dropdown=d.findElement(By.xpath("//form[@class='form-inline']/div[2]/div[@class='second_input']/select[@class='form-control']"));
(OR)
WebElement dropdown=d.findElement(By.xpath("//form[@class='form-inline']/div[2]/div[@class='second_input']/select[@name='country_id']"));
List<WebElement> w= dropdown.findElements(By.tagName("option"));
System.out.println(w.size());
Number of options -- 253
Sir, the code I gave you doesn't contain any xpath. I am locating it using name.
WebElement dropList=driver.findElement(By.name("country_id"));
List<WebElement> options=dropList.findElements(By.tagName("option"));
System.out.println("Total number of options are: "+options.size());
Even using ((By.name("country_id"))) should fetch you correct result
[attachment=0:3tw2jcj4]country.png[/attachment:3tw2jcj4]
Please find the attached file. I am still getting zero. Not able to find the mistake
WOW.. That worked.
How is that possible. How can a browser affect the code?
Is it the firefox version compatibility issue?