将子元素添加到 Selenium 的 html 后,在页面对象中找不到子元素
Child elements not found in page object after adding them in the html in Selenium
我的程序中有一个循环,它在每次迭代中打印如下结构:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<button>show more</button>
</div>
</div>
但点击按钮后结构变为:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<myTag>2</myTag>
<myTag>3</myTag>
</div>
</div>
所以在我对页面对象模型的测试中,我试图评估每个 "grand-father" 元素中的每个 myTag 元素,方法是:
List<WebElement> grandFathers = driver.findElements(By.xpath("//div[@class='grand-father']"));
for(WebElement gf : grandFathers){
//**Click the button of this father element to show more**
List<WebElement> myTagElements = gf.findElements(By.xpath(".//div[@class='father']/myTag"));
System.out.println(myTagElements.size());
}
我的问题是 myTag 元素的最后一个 findElements 似乎没有找到在按下 "show more" 按钮之前不存在的所有元素:( 它只计算一个元素,即使所有元素都已显示:/
有什么方法可以告诉 selenium "update" 变量根据我们第一次获取元素时不存在的新 html 更改? (因为我觉得问题可能出在那个)
谢谢大家帮忙!
我通过重构所有代码并在对 "father" 元素使用 findElements 时单击 "Show More" 按钮解决了这个问题,在对 myTag[ 使用 findElements 方法之前=13=]个元素。
在处理最终会出现在您的 DOM 中的元素时,请记住这一点:首先进行所有涉及显示和处理数据的操作,然后使用 Selenium 对其求值:)
看起来你可能已经有了答案,但对于未来的读者,我会做这样的事情......
抓取所有带有 class grand-father
的 DIV
并循环查找子 BUTTON
。单击 BUTTON
,然后计数 myTag
。
List<WebElement> gfs = driver.findElements(By.cssSelector("div.grand-father"));
for (WebElement gf : gfs)
{
gf.findElement(By.tagName("button")).click();
System.out.println(gf.findElements(By.tagName("myTag")).size());
}
我的程序中有一个循环,它在每次迭代中打印如下结构:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<button>show more</button>
</div>
</div>
但点击按钮后结构变为:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<myTag>2</myTag>
<myTag>3</myTag>
</div>
</div>
所以在我对页面对象模型的测试中,我试图评估每个 "grand-father" 元素中的每个 myTag 元素,方法是:
List<WebElement> grandFathers = driver.findElements(By.xpath("//div[@class='grand-father']"));
for(WebElement gf : grandFathers){
//**Click the button of this father element to show more**
List<WebElement> myTagElements = gf.findElements(By.xpath(".//div[@class='father']/myTag"));
System.out.println(myTagElements.size());
}
我的问题是 myTag 元素的最后一个 findElements 似乎没有找到在按下 "show more" 按钮之前不存在的所有元素:( 它只计算一个元素,即使所有元素都已显示:/
有什么方法可以告诉 selenium "update" 变量根据我们第一次获取元素时不存在的新 html 更改? (因为我觉得问题可能出在那个)
谢谢大家帮忙!
我通过重构所有代码并在对 "father" 元素使用 findElements 时单击 "Show More" 按钮解决了这个问题,在对 myTag[ 使用 findElements 方法之前=13=]个元素。 在处理最终会出现在您的 DOM 中的元素时,请记住这一点:首先进行所有涉及显示和处理数据的操作,然后使用 Selenium 对其求值:)
看起来你可能已经有了答案,但对于未来的读者,我会做这样的事情......
抓取所有带有 class grand-father
的 DIV
并循环查找子 BUTTON
。单击 BUTTON
,然后计数 myTag
。
List<WebElement> gfs = driver.findElements(By.cssSelector("div.grand-father"));
for (WebElement gf : gfs)
{
gf.findElement(By.tagName("button")).click();
System.out.println(gf.findElements(By.tagName("myTag")).size());
}