Select 具有相同 ID 的多个元素 - WebDriverJS

Select multiple elements with same id - WebDriverJS

我必须使用 SeleniumWebDriverJSJasmine 创建一个测试,以便仅当按钮连接到 div 时才显示 div 的内容div 被点击,并隐藏所有其他具有相同 id 的元素。

This是需要测试的函数,这是我目前写的测试片段:

it('should display "BILL2" when the second picture is clicked and hide the others', function() {
    driver.findElement(webdriver.By.css('#img_bill2')).click()
        .then(function(){
            driver.findElement(webdriver.By.css('#bill2')).isDisplayed()
                .then(function(displayed) {
                    expect(displayed).toBe(true);
                });
        })
        .then(function() {
            driver.findElement(webdriver.By.xpath('//div[@class="hide", @style="display:none"]')).isDisplayed()
                .then(function(displayed) {
                    expect(displayed).toBe(false);
                })
        });
});

我被困在这里是因为我想 select 除了当前显示的元素之外的所有元素。

你知道如何解决这个问题吗?也许使用 executeScript() 并创建一个脚本来定位所有具有 display:none 样式的 divs 可以解决吗?

提前感谢您的回复!

我更愿意在不使用 .isDisplayed() 的情况下通过计算不可见元素的数量来解决此问题。由于所有元素都会有 display:none,这将确保不显示元素。

it('should display "BILL2" when the second picture is clicked and hide the others', function() {
    driver.findElement(webdriver.By.css('#img_bill2')).click()
        .then(function(){
            driver.findElement(webdriver.By.css('#bill2')).isDisplayed()
                .then(function(displayed) {
                    expect(displayed).toBe(true);
                });
        })
        .then(function() {
            driver.findElements(webdriver.By.xpath('//div[contains(@class, "hide") and contains(@style, "display: none")]'))
                .then(function(elements) {
                    expect(elements.length).toBe(expectedCount);
                })
        });
});