Module 13. Code not working | Selenium Forum
M
Posted on 28/12/2016
--> When I am running the following code, getting 2 problems
1. When I am sending keys in dropdown as "Egypt". It selects Taiwan. How is it possible?
2. When I am calculating the size of the dropdown list, it gives zero. Why is it happening

public class Dropdown {
public static void main(String[] args) {
WebDriver driver=null;
//select, count, print, find the selected
System.setProperty("webdriver.gecko.driver", "D:\\Madhur\\Selenium\\selenium-java-3.0.0-beta3\\geckodriver-v0.11.1-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://qtpselenium.com/home/contact_trainer");

WebElement dropList=driver.findElement(By.name("country_id"));
[b:2e83dnb3] dropList.sendKeys("Egypt");[/b:2e83dnb3]

[b:2e83dnb3]List<WebElement> options=dropList.findElements(By.tagName("option"));
System.out.println("Total number of options are: "+options.size());[/b:2e83dnb3]
}}

M
Replied on 28/12/2016

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");


M
Replied on 28/12/2016

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?


M
Replied on 28/12/2016

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)


M
Replied on 29/12/2016

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


M
Replied on 29/12/2016

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());


M
Replied on 29/12/2016

Even using ((By.name("country_id"))) should fetch you correct result

[attachment=0:3tw2jcj4]country.png[/attachment:3tw2jcj4]

Responsive image

M
Replied on 29/12/2016

Please find the attached file. I am still getting zero. Not able to find the mistake

Responsive image

M
Replied on 30/12/2016

can you pls try on different browser?


M
Replied on 01/01/2017

WOW.. That worked.
Responsive image Responsive image

How is that possible. How can a browser affect the code?
Is it the firefox version compatibility issue?


M
Replied on 01/01/2017

This might be some thing related to version compatibility issue....