简体   繁体   中英

Not able to locate the dropdown value using selenium webdriver

I want to select the value from dropdown.

Currently I am able to click on dropdown but not able to select the value from the dropdown. Below are the code which I am using for selecting the value from the dropdown.

                temp.click();
                driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
                Select clickThis = new Select(temp); 
                try{

                    Thread.sleep(5000);
                    clickThis.selectByValue("India");

                }catch(Exception e){
                    e.printStackTrace();
                    System.out.println("<><><><><>Not Found<><><><><><>");
                }

I am working it in framework, can you please let me know the code accordingly. Please check the below code which I am using.

private boolean operateWebDriver(String operation, String Locator,
            String value, String objectName) throws Exception {
        boolean testCaseStep = false;

        try {
            System.out.println("Operation execution in progress");
            WebElement temp = getElement(Locator, objectName);
            if (operation.equalsIgnoreCase("SendKey")) {
                temp.sendKeys(value);
            }
            Thread.sleep(1000);
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();
            }
            if (operation.equalsIgnoreCase("Verify")) {
                System.out.println("Verify--->" + temp);
                temp.isDisplayed();

            }
            if (operation.equalsIgnoreCase("clickDropdown")) {

                temp.click();
                driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
                Select clickThis = new Select(temp); 
                try{

                    Thread.sleep(5000);
                    clickThis.selectByValue("India");

                }catch(Exception e){
                    e.printStackTrace();
                    System.out.println("<><><><><>Not Found<><><><><><>");
                }
               }

            testCaseStep = true;

        } catch (Exception e) {
            System.out.println("Exception occurred operateWebDriver"
                    + e.getMessage());

            // Take screenshot if any testcase is not working. 
            System.out.println("Taking Screen Shot");
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\Simple page creator\\Snapshot\\screenshot.jpeg")); 
        }

        return testCaseStep;
    }

    public WebElement getElement(String locator, String objectName)
            throws Exception {
        WebElement temp = null;

        System.out.println("Locator-->" + locator);
        if (locator.equalsIgnoreCase("id")) {
            temp = driver.findElement(By.id(objectName));

        } else if (locator.equalsIgnoreCase("xpath")) {
            temp = driver.findElement(By.xpath(objectName));
            System.out.println("xpath temp ----->" + temp);
        } else if (locator.equalsIgnoreCase("name")) {
            temp = driver.findElement(By.name(objectName));
        }
        return temp;

    }

}

HTML

<select id="billing_country" name="billing_country">
<option value="">Choose Country</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="AG">Algeria</option>
<option value="AQ">American Samoa</option>
<option value="AN">Andorra</option>
<option value="AO">Angola</option>
<option value="AV">Anguilla</option>
<option value="AY">Antarctica</option>
<option value="AC">Antigua and Barbuda</option>
<option value="AR">Argentina</option>
<option value="AM">Armenia</option>
<option value="AA">Aruba</option>
<option value="AT">Ashmore and Cartier</option>
<option value="AS">Australia</option>
<option value="AU">Austria</option>
<option value="AJ">Azerbaijan</option>
<option value="BF">The Bahamas</option>
<option value="BA">Bahrain</option>
<option value="FQ">Baker Island</option>
<option value="BG">Bangladesh</option>

Looking by the HTML code,I assume India is the text of the option like below:

<option value="IN">India</option> 

For this scenario please try with the following code similar to Saifur's reply but with different method.

By element = driver.findElement(By.id("billing_country"));
Select foo = new Select(element);
foo.selectByVisibleText("India"); 

If you want to go with your framework, please share the exception webdriver throws. As the country list is too big, option India may not be visible for the webdriver to click and would have thrown exception similar as: org.openqa.selenium.WebDriverException: Element is not clickable at point (XXX, YYY). Other element would receive the click:

First, you have numerous mix up of different kind of waits which should not be done. To find an element and check it's existence the explicit wait works most of the case. Mixing up thread.sleep and implicit wait will give you some really bad performance of test execution

Second, finding dropdown using Select class is the best. You can do the following which is simple and easy to debug as well.

By element = driver.findElement(By.id("billing_country"));
Select foo = new Select(element);
foo.selectByValue("AF"); //should select Afghanistan. I do not see India

See the API doc here

driver.findElement(By.id("billing_country")).click();
driver.findElement(By.xpath("//*[@id='billing_country']//*[contains(., 'India')]")).click();

Try this. See if it works. You could substitute the '*'s out for the actual tags, but I don't see a reason for that unless your id of "billing_country" exists elsewhere.

edit:

As mentioned elsewhere, you may need to ensure the item is clickable before actually clicking it, in which case, you can use javascript to scroll the item into view.

In your html, <option value="IN">India</option> should be for India.

You have : clickThis.selectByValue("IN");

If you try clickThis.selectByVisibleText("India"); t will work.

driver.findElement(By.xpath("//*[@id='billing_country']//*[contains(., 'India')]")).click();

通过使用该测试用例,只能选择一个值,以防万一如果我给了另一个值而不是“ India”,但它本身就是印度,请告诉我是否对此测试用例进行了任何更正

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM