java - 在 Selenium webdriver 中跳过 SVG 条形图上的元素

标签 java eclipse selenium selenium-webdriver webdriver

我已经在 java 中为 selenium 构建了一个方法,我想单击 SVG 条形图并单击前 3 个条形图(下面的屏幕截图) Screenshot 我通过实现以下代码来完成此操作:

    public static void barChartSelector(InternetExplorerDriver driver)
{
    genericControls.waitCommands.fluentWaitOnBarChartSelector(driver);
    WebElement parent = driver.findElement(By.className("highcharts-series-group"));
    genericControls.waitCommands.fluentWaitOnBarChartSelector(driver);
    List<WebElement> children = parent.findElements(By.tagName("rect"));
    genericControls.waitCommands.fluentWaitOnBarChartSelector(driver);
    children.get(0).click();
    children.get(1).click();
    children.get(2).click();
    genericControls.waitCommands.fluentWaitOnRelationalBarGraphDisplay(driver);
}

但是,我发现有时会出现此错误:

"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at genericControls.graphSelectors.relationalBarChartSelector(graphSelectors.java:34)
    at Reports.collections.breachReportCompletedFunctions(collections.java:30)
    at Reports.programMain.main(programMain.java:37)"

看起来,当我运行 get 命令时,图表在各个页面上的索引并不总是相同,因此在某些页面上它可以工作,而在某些页面上它会失败。我的想法是,我需要实现一个 for 循环,以便我仍然可以运行 children.get(X).click(); 事件,而不是摔倒如果代码找不到第一个事件,它将循环执行,直到找到返回结果的 get 命令。

请有人告诉我如何将上面的代码转换为 for 循环,以便它可以查找任何可能满足条件的 children 元素?这将帮助我了解将来如何实现这一点。

最佳答案

在我看来问题就在这里:

List<WebElement> children = parent.findElements(By.tagName("rect"));

抛出 IndexOutOfBoundsException 是因为 children 并不总是包含 3 个 WebElement。

尝试用这个替换:

List<WebElement> children = parent.findElements(By.tagName("rect"));
genericControls.waitCommands.fluentWaitOnBarChartSelector(driver);
for (WebElement child : children) {
    if (children.indexOf(child) > 2)
        break;
    child.click();
}

注意,我假设可能有超过 3 个“矩形”WebElement,并且您只对单击前 3 个感兴趣。如果您想单击所有矩形元素,请删除 if 语句。

关于java - 在 Selenium webdriver 中跳过 SVG 条形图上的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506816/

相关文章:

eclipse - eclipse 中的编辑器消失了

python - 使用 python/selenium 从元素链接中提取部分文本

java - 记录异常 - 如何充分利用它们

java - 我该如何更换这个?

java - toString() 方法问题

java - 将netbeans导入到eclipse中

java - 我可以在 Eclipse Java 项目运行配置中设置任意命令吗?

javascript - Nightwatchjs获取多个输入的值

java - org.openqa.selenium.UnhandledAlertException : unexpected alert open

java - Java 中的线程最大数量?