如何使用 selenium webdriver 单击自定义 Web 组件

How to click on custom web component using selenium webdriver

我刚开始编写 selenium 测试,请多多包涵。我正在尝试为我们主页上的旋转木马元素编写一个自动化测试。我希望测试单击轮播导航按钮之一,然后验证是否已将某些内联样式添加到轮播 div。 (该样式将导致旋转木马屏幕移动并显示不同的项目。)导航按钮是一个自定义 Web 元素,而不是一个或一个或其他明显可点击的东西。我的测试在 click() 上失败,显示 "WebDriverError: element not interactable"。这是因为 Selenium 无法将我的自定义 Web 元素识别为可点击的东西吗?还是我做错了什么?

我进行了大量谷歌搜索并尝试了许多不同的语法,包括添加睡眠(我知道这是一个很大的禁忌)。我还验证了 xpath 是正确的,因为当我将 xpath 更改为其他内容时,我得到了一个不同的错误,指出找不到该元素。

HTML:

<u-carousel id="hero-carousel">
    <div class="u.container"><!-- this is the element that gets the inline style added to -->
    ...carousel content here...
    </div>
</u-carousel>

<u-carousel-move class="home.hero.others active" u-for="hero-carousel" u-to="2">
    <div class="carousel-nav-button">
        Nav Item 2
    </div>
</u-carousel-move>
<u-carousel-move class="home.hero.others" u-for="hero-carousel" u-to="2">
    <div class="carousel-nav-button">
        Nav Item 3
    </div>
</u-carousel-move>

硒Javascript:

describe('homepage carousel', () => {
    test('carousel displays', async () => {
        expect(await carouselAppPicker()).toBe(true);
    });
});

async carouselAppPicker() {
    console.info({action: 'carouselAppPicker', status: 'run'});
    let app_picker_xpath = '//u-carousel-move[contains(@class,"home.hero.others")][@u-for="hero-carousel"][@u-to="2"]';
    let app_picker = await this.driver.wait(until.elementLocated(By.xpath(app_picker_xpath)), 5000, 'Cannot find app picker element');
    await app_picker.click();
    let carousel_screen_xpath = '//u-carousel[@id="hero-carousel"]/div[@class="u.container"][@style="transform: translateX(-200%);"]';
    await this.driver.wait(until.elementLocated(By.xpath(carousel_screen_xpath)), 5000, 'Cannot find screen element');
    return true;
}

这是我在使用 jest 从命令行 运行 此测试时遇到的错误:

 homepage carousel › carousel displays

    WebDriverError: element not interactable
      (Session info: headless chrome=73.0.3683.103)
      (Driver info: chromedriver=2.46.628411 (hash),platform=Mac OS X 10.13.6 x86_64)

      at Object.checkLegacyResponse (node_modules/selenium-webdriver/lib/error.js:585:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:533:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:468:26)

尝试使用javascript点击。

driver.executeScript("arguments[0].click()",app_picker)