简体   繁体   English

无法使用Selenium WebDriver找到下拉值

[英]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 的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: 通过HTML代码查看,我假设印度是如下所示的选项的文本:

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

For this scenario please try with the following code similar to Saifur's reply but with different method. 对于这种情况,请尝试使用与Saifur的回复类似的以下代码,但使用不同的方法。

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. 如果要使用您的框架,请共享Webdriver引发的异常。 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). 由于国家/地区列表太大,因此对于选项卡印度来说,对于网络驱动程序而言,单击可能不可见,并且会引发类似以下异常:org.openqa.selenium.WebDriverException:元素在点(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 混合使用thread.sleepimplicit等待会给您一些非常糟糕的测试执行性能

Second, finding dropdown using Select class is the best. 其次,使用Select类查找下拉列表是最好的。 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 请在此处查看API文档

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. 您可以用'*'代替实际的标签,但是除非您的“ billing_country”的ID在其他地方存在,否则我看不出原因。

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. 如在其他地方提到的,您可能需要确保在实际单击项目之前可单击该项目,在这种情况下,可以使用javascript将项目滚动到视图中。

In your html, <option value="IN">India</option> should be for India. 在您的html中, <option value="IN">India</option>应该用于印度。

You have : clickThis.selectByValue("IN"); 您具有: clickThis.selectByValue("IN");

If you try clickThis.selectByVisibleText("India"); 如果尝试clickThis.selectByVisibleText("India"); t will work. 它会起作用。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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