繁体   English   中英

无法使用Selenium WebDriver找到下拉值

[英]Not able to locate the dropdown value using selenium webdriver

我想从下拉列表中选择值。

目前,我可以单击下拉列表,但不能从下拉列表中选择值。 下面是用于从下拉列表中选择值的代码。

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

我正在框架中工作,请您让我知道相应的代码。 请检查以下我正在使用的代码。

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>

通过HTML代码查看,我假设印度是如下所示的选项的文本:

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

对于这种情况,请尝试使用与Saifur的回复类似的以下代码,但使用不同的方法。

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

如果要使用您的框架,请共享Webdriver引发的异常。 由于国家/地区列表太大,因此对于选项卡印度来说,对于网络驱动程序而言,单击可能不可见,并且会引发类似以下异常:org.openqa.selenium.WebDriverException:元素在点(XXX,YYY)不可单击。 其他元素将获得点击:

首先,您会混合许多不应该进行的不同类型的等待。 要查找元素并检查其是否存在,显式等待在大多数情况下适用。 混合使用thread.sleepimplicit等待会给您一些非常糟糕的测试执行性能

其次,使用Select类查找下拉列表是最好的。 您还可以执行以下简单且容易调试的操作。

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

请在此处查看API文档

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

尝试这个。 看看是否可行。 您可以用'*'代替实际的标签,但是除非您的“ billing_country”的ID在其他地方存在,否则我看不出原因。

编辑:

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

在您的html中, <option value="IN">India</option>应该用于印度。

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

如果尝试clickThis.selectByVisibleText("India"); 它会起作用。

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