如何使用Selenium WebDriver单击自定义Web组件



我是写硒测试的新手,所以请忍受我。我正在尝试在主页上为轮播元件编写自动测试。我希望测试点击一个轮播导航按钮,然后验证旋转木马div中是否添加了一些内联样式。(该样式将导致轮播屏幕移动并显示不同的项目。(导航按钮是自定义的Web元素,而不是A或其他明显可单击的内容。我的测试在单击((上失败,说" WebDriverError:element不可交互"。这是因为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)

最新更新