非常简单的问题,基本上我试图在单击另一个链接后获取 li 元素的文本。该链接将导致 li 命令更改。每当我尝试获取文本时,它都会在订单更改之前返回文本。单击链接更改订单后如何获取 li 文本?该链接不会重新加载页面本身。
这是我用来获取元素的内容:
driver.findElement(By.cssSelector("li[id^='coupon_']:nth-of-type(1) p.brand")).getText();
更改本身将来自:
<div id="id1">
<ul class="pods">
<li rel="" title="some text" id="coupon_1" data-pos="1">
...
<p class="brand">brand1</p>
...
</li>
</ul>
</div>
到:
<div id="id1">
<ul class="pods">
<li rel="" title="some text" id="coupon_5" data-pos="1">
...
<p class="brand">brand5</p>
...
</li>
</ul>
</div>
我有类似的情况,我会使用 wait.until 来让发生变化的元素出现然后获取它,但在这种情况下,li 元素已经存在,但只是在单击链接后发生变化。我怎样才能得到新的元素文本?谢谢!
最佳答案
你可以在li
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait for the new li to appear
WebElement li = wait.until(ExpectedConditions.elementToBeClickable(By.id("coupon_5")));
System.out.println(li.findElement(By.cssSelector("p.brand")).getText().trim());
或者如果新 id 有多种可能性,那么您可以等待旧 id 元素变得陈旧。
// get the original li that will be changed
WebElement oldLi = driver.findElement(By.id("coupon_1"));
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait for the original li to go stale... change
wait.until(ExpectedConditions.stalenessOf(oldLi));
// grab the new element by whatever the new id is
WebElement newLi = driver.findElement(By.id("coupon_5"));
System.out.println(newLi.findElement(By.cssSelector("p.brand")).getText().trim());
关于java - Selenium webdriver 在页面更改后获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32408188/