gpt4 book ai didi

java - Selenium java如何从第二个引号中的元素获取文本 ""

转载 作者:行者123 更新时间:2023-12-01 18:13:16 26 4
gpt4 key购买 nike

<div class="_2b9p7-6Tcy_ja6zEhxML2e">
<div class="flex _1VLiOJeD-kdBBLC9owCkQr">
<span class="flex-1">”Total””:”</span>
<span>
“USD”
“2,175”
</span>

需要将文本放在第二个引号“”中,在本例中为““2,175””,但我不知道该怎么做。“USD”和“2,175”都来自API(每次都不一样,因此我不能使用driver.findElement(By.xpath("xpath and not 'USD'),我可以分割结果,只取必要的部分,但我想知道如何从 future 的第二个括号中获取值(2,175)。我在 Intellij 上执行了以下操作

System.out.println(driver.findElement(By.xpath("//div[@class = '_2b9p7-6Tcy_ja6zEhxML2e']//div//span/following-sibling:: span")).getText());

当然,还有全文 - 2,175 美元,但我需要 2,175 美元

最佳答案

要提取值2,175,您需要引发WebDriverWait以使所需的元素可见,并且您可以使用以下任一解决方案:

  • 使用xpath以下:

    WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Total')]//following::span[1]")));
    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString());
  • 使用xpath以下兄弟:

    WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Total')]//following-sibling::span[1]")));
    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString());

关于java - Selenium java如何从第二个引号中的元素获取文本 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60422447/

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