Eclipse Java Selenium webdriver - 无法选择下拉项(ElementNotVisibleException) [英] Eclipse Java Selenium webdriver - Unable to select dropdown item (ElementNotVisibleException)

查看:170
本文介绍了Eclipse Java Selenium webdriver - 无法选择下拉项(ElementNotVisibleException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的菜单中有一个下拉列表,我想在点击下拉菜单后选择两个可用选项之一。我无法获得显示的选项,因此我无法单击任何一个。
这是页面上下拉列表的源代码:

I have a dropdown list in my menu and I'm trying to select one of two options available after clicking the dropdown. I can't get the options to show though and therefore I'm unable to click either one. This is the source code of the dropdown list on the page:

<span class="ribbondropdown combined importantribbonbutton">
<div class="button_ribbon hasimage ">
<div id="ribbon_nav_dashboard_dropper" class="button_ribbon_dropper ">
<div class="inner"></div>

<div class="button_ribbon_dropdownsection" style="left: 1520px; top: 40px; display: block;">
<table class="dropdownlist" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="icon"></td>
<td class="value">Option 1</td>
</tr>
<tr>
<td class="icon"></td>
<td class="value">Option 2</td>
</tr>
</tbody>
</table>
</div>
</div>
</span>

前四行是我要点击的区域。代码的其余部分是单击后可见的部分,此处我想单击选项2。

The first four lines are the area I want to click. the rest of the code is for the section that becomes visible after clicking and here I'd like to click 'Option 2'.

我使用以下命令进行修改:

I'm currenlty using the following:

driver.findElement(By.id("ribbon_nav_dashboard_dropper")).click();
driver.findElement(By.xpath("//div[@class='value' and text()='Option 2']")).click();

这会导致失败:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

$ b进行交互
$ b

如果我注释第二行,只需点击下拉列表显示选项,并使测试显示其不显示的选项。如果我手动这样做,我会得到两个选项。我已经看过另外一个异常的问题,但是提出的解决方案对我来说不起作用: Java webdriver:元素不可见异常

推荐答案

我将开始告诉你,我正在学习HTML格式和语法,所以你可能会发现我正在围绕一个问题走。我试图根据你的样本在本地创建一个基本的网页,我无法创建任何验证这个提案的下拉列表。

I will start by telling you that I am learning html formatting and syntax so you may find that I'm taking the long-way around a problem. I tried creating a basic webpage locally based on your sample and I was not able to create the drop-down for any validation of this proposal.

这就是说...


如果我注释掉第二行,只需点击
的下拉列表显示选项,并使测试显示选项不显示。

"If I comment out the second line, only clicking on the dropdown to show the options and make the test show the options it does not show."

专注于此。如果您无法显示该元素,那么您将永远无法点击它。

根据描述的行为,我认为您的组合ID查找可能是结构中的错误元素。

Based on the described behavior, I think your combo id lookup is perhaps the wrong element in the structure.

By.id("ribbon_nav_dashboard_dropper")

下面的Junit类基本上是如何在下拉菜单中找到正确的元素。填写 buildLookups 方法中的String数组,测试用例应该告诉您您输入的选项将允许单击选项[2]。

The Junit class below is basically how I would go about testing if I've got the right element for the drop down. Fill in the String arrays in the buildLookups method and the test cases should tell you which of the options you've entered will allow the 'option[2]' to be clicked.

在这种情况下,我还实施了执行 performValidation 方法的工作,但是我建议双重检查,以确保我已经了解了问题正确。

I also took a crack at implementing how the performValidation method would work in this case, but I'd recommend double checking that just to be sure I've understood the problem correctly.

package stackoverflow.proof.selenium;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Parameterized test which can be used to validate lookups for a single well-defined desired interaction in Selenium.
 * 
 */
@RunWith(Parameterized.class)
public class SeleniumLookupValidationUtil {
    /** Test URL where the element should be validated.*/
    private static final String TEST_URL = "myURL";
    /** Function which converts a String into a {@link By#id(String)} reference.*/
    private static final Function<String, By> AS_ID_LOOKUP = new Function<String, By>() {
        @Override
        public By apply(String arg0) {
            System.out.println("Id Lookup: " + arg0);
            return By.id(arg0);
        }
    };
    /** Function which converts a String into a {@link By#xpath(String)} reference.*/
    private static final Function<String, By> AS_XPATH_LOOKUP = new Function<String, By>() {
        @Override
        public By apply(String arg0) {
            System.out.println("xpath Lookup: " + arg0);
            return By.xpath(arg0);
        }
    };
    /** Function which converts a String into a {@link By#cssSelector(String)} reference.*/
    private static final Function<String, By> AS_CSS_SELECTOR_LOOKUP = new Function<String, By>() {
        @Override
        public By apply(String arg0) {
            System.out.println("css Lookup: " + arg0);
            return By.cssSelector(arg0);
        }
    };
    /** Function which converts a String into a {@link By#className(String)} reference.*/
    private static final Function<String, By> AS_CLASS_NAME_LOOKUP = new Function<String, By>() {
        @Override
        public By apply(String arg0) {
            System.out.println("className Lookup: " + arg0);
            return By.className(arg0);
        }
    };
    /** Function which converts a String into a {@link By#linkText(String)} reference.*/
    private static final Function<String, By> AS_LINK_TEXT_LOOKUP = new Function<String, By>() {
        @Override
        public By apply(String arg0) {
            System.out.println("LinkText Lookup: " + arg0);
            return By.linkText(arg0);
        }
    };

    /**
     * Creates the data for running the test instance.
     * @return Collection of Object arrays.  Each array in the collection represents a different test execution.
     */
    @Parameters(name="{0}")
    public static Collection<Object[]> buildLookups() {
        //TODO:  Fill in add as many possible lookups for the drop-down element as you can find.
        String[] ids = new String[]{"id1", "id2"};
        String[] xpaths = new String[]{};
        String[] cssSelectors = new String[]{};
        String[] classNames = new String[]{"A", "B"};
        String[] linkTexts = new String[]{};

        //I use the map so I can loop through the dataset later.
        Map<Function<String, By>, String[]> associations = Maps.newHashMap();
        associations.put(AS_ID_LOOKUP, ids);
        associations.put(AS_XPATH_LOOKUP,xpaths);
        associations.put(AS_CSS_SELECTOR_LOOKUP, cssSelectors);
        associations.put(AS_CLASS_NAME_LOOKUP, classNames);
        associations.put(AS_LINK_TEXT_LOOKUP, linkTexts);

        List<Object[]> parameters = Lists.newArrayList();
        for (Function<String, By> converter: associations.keySet()) {
            String[] lookupStrings = associations.get(converter);
            for (String lookup : lookupStrings) {
                By by = converter.apply(lookup);
                parameters.add(new Object[]{by});
            }
        }

        return parameters;
    }

    /** The By lookup to use for the current test validation.*/
    private final By target;
    /** WebDriver for testing.*/
    private WebDriver driver;

    /**
     * Test constructor.
     * @param lookup By to be used for this validation.
     */
    public SeleniumLookupValidationUtil(By lookup) {
        this.target = lookup;
    }

    /**
     * Creates the webdriver and establishes our validation state.
     */
    @Before
    public void initWebDriver() {
        driver = new FirefoxDriver();
        driver.get(TEST_URL);
        //TODO:  Any other setup to make the test case valid.
    }

    /**
     * Performs the thing we actually want to know about.
     */
    @Test
    public void performValidation() {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(target));
        WebElement option2 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='value' and text()='Option 2']")));
        option2.click();
    }

    /**
     * Closes all test browser windows and destroys the driver reference.
     */
    @After
    public void destroyWebDriver() {
        for (String window : driver.getWindowHandles()) {
            driver.switchTo().window(window);
            driver.close();
        }
    }
}

我使用google.guava api相当一点,你需要这个库来运行这个代码。以下是我用于该引用的maven pom依赖关系:

I use the google.guava api quite a bit, you will need that library to run this code. Here is the maven pom dependency I use for that reference:

 <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>LATEST</version>
      <scope>compile</scope>
    </dependency>

Best of Luck。

Best of Luck.

这篇关于Eclipse Java Selenium webdriver - 无法选择下拉项(ElementNotVisibleException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆