[英]how to get changed text in java selenium
<table>
<tr>
<td>hi</td>
</tr>
</table>
<button id="change" onclick="change()">change</button>
<script>
function change(){
$("table").empty();
var $tr = $("<tr></tr>");
$tr.append( $("<td>bye</td>") );
}
</script>
WebDriver driver=new ChromeDriver(chromeOptions);
WebElement change= driver.findElement(By.id("change"));
change.click();
WebElement td=driver.findElement(By.xpath("//*table/tr/td[1]"));
System.out.println(td.getText());
我期待“再見”,但它打印了“嗨”。
如何獲得動態更改的文本?
通過使用WebDriverWait
顯式等待,您可以獲得初始元素文本內容。 然后等待該文本在單擊后不再出現在該元素中,然后獲取新的元素文本內容。 如下:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement td =driver.findElement(By.xpath("//*table/tr/td[1]"));
String oldText = td.getText();
driver.findElement(By.id("change")).click();
wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*table/tr/td[1]"), oldText)));
System.out.println(td.getText());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.