首页 > 解决方案 > 按可用 ID 单击元素

问题描述

我有一个奇怪的情况,其中 Angular 元素使用 2 个随机 ID 呈现 html:

WebDriverWait webDriverWait = new WebDriverWait(driver, 5);
        System.out.println("Click on Sub Tab " + title_id + " using id locator " + tab_id);
        WebElement webElement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("mat-tab-label-5-0")));
        webElement.click();

或者

WebDriverWait webDriverWait = new WebDriverWait(driver, 5);
        System.out.println("Click on Sub Tab " + title_id + " using id locator " + tab_id);
        WebElement webElement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("mat-tab-label-3-0")));
        webElement.click();

顺序是随机的,我需要一些方法来点击可用的 id。有什么方法可以结合,例如:

By.id("mat-tab-label-3-0" OR "mat-tab-label-5-0")

有什么解决办法吗?

标签: javaseleniumselenium-webdriver

解决方案


您可以切换到 CSS 选择器。

ExpectedConditions.elementToBeClickable(By.cssSelector("#mat-tab-label-3-0,#mat-tab-label-5-0"))

#表示 ID 并,表示 OR。


推荐阅读