繁体   English   中英

如何在 java selenium 中更改文本

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM