簡體   English   中英

如何在不使用contains()和xpath的情況下從標簽中檢索特定文本?

[英]How to retrieve specific text from a tag without using contains() and xpath?

<div>
    <label1>Hulk</label1>
    <label2>Ironman</label2>
Thor
</div>

如何在不使用xpath中包含的情況下僅獲取Thor文本?

如果我嘗試獲取div的文本,它將給我全部3個(綠巨人,鋼鐵俠,雷神),但我只想要雷神。

要在不使用xpath中包含內容的情況下提取文本Thor ,可以使用以下解決方案之一:

  • xpath

     String myText = driver.findElement(By.xpath("//div[not(@label)]")).getText(); 
  • cssSelector

     String myText = driver.findElement(By.cssSelector("div:not(label)")).getText(); 
  • 使用JavaScriptExecutor

     WebElement myElement = driver.findElement(By.xpath("//div[@attribute='value']")); //replace the pseudo attribute with actual attribute String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString(); 

注意 :確保xpath作為//div[not(@label)]cssSelector作為div:not(label)與HTML DOM上的唯一元素相似。

您可以嘗試以下提到的路徑嗎?

/div/text()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM