【问题标题】:unable to locate element if element is dynamic如果元素是动态的,则无法定位元素
【发布时间】:2020-03-24 02:04:32
【问题描述】:

我收到错误消息,即使它是从 DD 中选择的,也无法找到元素

我的元素是动态的。

这个方法我试过了,

         static class GroupCount {
                    private static int i = 1;

                    static void inc() {
                        i++;
                    }

                    static int get() {
                        return i;
                    }
                }

                String xpathGroupSelect = "//*[@id='groupPicker" +
                                    GroupCount.get() + "-list']//div[contains(concat(' ','x-combo-list-item', ' '), 'x-combo-list-item') " +
                                    "and contains(text(),'"
                                    + name + "')]";

         WebElement group = driver.findElement(By.xpath(xpathGroupSelect));

     if (xpathGroupPick.isDisplayed()) {

                    WebDriverWait wait = new WebDriverWait(driver, 30);

                     wait.until(ExpectedConditions.elementToBeClickable(group)).click();


                    group.click();

WorkflowAction.addApprovers("Compliance");

我正在寻找“合规”元素

这是我的html代码:

> <div class="x-combo-list-inner" id="groupPicker1-list" style="width:
> 218px; margin-bottom: 8px; height: 81px;">    <div
> class="x-combo-list-item" style="">APPROVAL</div> <div
> class="x-combo-list-item" style="">Compliance</div>   <div

【问题讨论】:

  • 您要查找哪个元素?
  • 我正在尝试完善“合规”元素

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

要定位文本为 Compliance 的元素,您可以为 visibilityOfElementLocated() 诱导 WebDriverWait,您可以使用以下任一 Locator Strategies

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.x-combo-list-inner[id^='groupPicker'] div:nth-child(2)")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='x-combo-list-inner' and starts-with(@id, 'groupPicker')]//div[@class='x-combo-list-item' and text()='Compliance']")));
    

【讨论】:

  • 这里 id 是动态的,它会改变所以不能使用 'gropupicker1'
  • 检查更新的答案并告诉我状态,现在它应该与 gropupicker1gropupicker2gropupicker3 一起使用等
猜你喜欢
  • 1970-01-01
  • 2011-09-22
  • 2019-12-12
  • 2017-11-16
  • 2012-01-11
  • 2018-01-30
相关资源
最近更新 更多