gpt4 book ai didi

java - 在表格行中查找空 Div 的标题 - Selenium Java

转载 作者:行者123 更新时间:2023-11-30 08:10:05 26 4
gpt4 key购买 nike

有一个 Div 类和标题在这个表格行中经常变化。我需要在 div 读取特定内容时捕捉它。

代码如下所示:

<tr class="this_row">
<td class="something"></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="i_need_this" title="or This"></div>
</td>
</td></td>
<td class="blah"></td>

等等

现在我的代码是这样的:

findElement(By.xpath("//tr[contains(@class, 'this_row')//div[contains(@class, 'i_need_this')]")).getText().contains("This text matches exactly within td class=blah from above"))

这是行不通的。

有什么建议吗?

最佳答案

这听起来像是用 WebDriverWait 显式等待的工作和预期条件:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement td = wait.until(ExpectedConditions.textToBePresentInElement(By.cssSelector("tr.this_row td.blah"), "This text matches exactly within td class=blah from above"));

在这里,我们正在等待最多 10 秒,以便This text matches exactly within td class=blah from above 文本出现在td 类为 blah 的元素位于类为 this_rowtr 元素中。

或者,我们也可以等待特定的 div 元素出现:

WebElement div = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("tr.this_row > td > div")));
System.out.println(div.getAttribute("title"));

关于java - 在表格行中查找空 Div 的标题 - Selenium Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com