簡體   English   中英

帶有 Java 的 WebDriver - 無法使用 Webdriver 獲取所有文本

[英]WebDriver with Java - Not able to get all the text using Webdriver

我無法獲取此網站上的所有文本 - https://niftygateway.com/marketplace

使用這個簡單的代碼 -

String iterativeXpath = "(//*[@id='root']/div/div[2]/div[2]/div/div[2]/div[2]/div[1]/div/div)";
        iterativeXpath = iterativeXpath.substring(0, iterativeXpath.length()-1);
        WebDriverWait wait = new WebDriverWait(driver, 15);
        for(int i = 1; i <=20; i++){
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(iterativeXpath+"["+i+"])")));
           System.out.println(driver.findElement(By.xpath(iterativeXpath+"["+i+"])")).getText());
        }

要獲取此網站https://niftygateway.com/marketplace上的所有文本,您可以使用以下定位器策略

  • 使用cssSelector

     driver.get("https://niftygateway.com/marketplace"); System.out.println(driver.findElements(By.cssSelector("div.MuiCardContent-root")).stream().map(element->element.getText()).collect(Collectors.toList()));

理想情況下,您需要為visibilityOfAllElementsLocatedBy()引入WebDriverWait ,並且可以使用以下Locator Strategy

  • 使用xpath

     driver.get("https://niftygateway.com/marketplace"); System.out.println(new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[contains(@class, 'MuiCardContent-root')]"))).stream().map(element->element.getText()).collect(Collectors.toList()));

控制台 Output:

[ATLAS #2/21
Symbiocene Mythologica
$1,333.00, The Flipper #86/99
The Title by Pak
Make Offer, A Trebled Man #7/20
Select Works by Alotta Money
$1,850.00, UNISWAP #164/261
Banned From The Internet Open Edition by Slime Sunday
$725.00, Peng - Shiny #5/5
Crystal Pops - Winter Edition by Goldweard
$1,500.00, Gucci Ghost Aqua Pink #17/20
Nifty Ghost Collection by Trevor Andrew
$2,500.00, Entangled #7/15
Tranquility by Andreas Wannerstedt
$333.33, The Day I Decided to Fly #72/268
Growing Up...I'm Scared Open Edition by FEWOCiOUS
$413.00, Extrusion #447/457
The Collision by Pak x Trevor Jones
Make Offer, A Trebled Man #13/20
Select Works by Alotta Money
$1,888.88, The Square #5/6
The Japanese Garden by Six n Five
$2,500.00, The Last Stand of the Nation State
Open Edition by Slimesunday
$1,325.00, Kikai Ningyou #16/20
Twisted Vacancy Edition
Not Accepting Offers, Extrusion #63/457
The Collision by Pak x Trevor Jones
Not Accepting Offers, The Sprite
Metamorphosis Open Edition By Metageist
$178.88, Inu - Shiny #15/15
Crystal Pops Asia Edition
$358.88, Pandy - Shiny #14/40
Crystal Pops Asia Edition
$228.88, Peng #24/30
Crystal Pops - Winter Edition by Goldweard
$288.88, Tiggz - Shiny #39/75
Crystal Pops Asia Edition
$125.88, A Trebled Man #20/20
Select Works by Alotta Money
$2,500.00]

要等待列表可以使用visibilityOfAllElementsLocatedBy

並且div.MuiCardContent-root.jss414選擇器看起來更好:

driver.get("https://niftygateway.com/marketplace");
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.MuiCardContent-root.jss414")));

for(WebElement element: elements) {
    System.out.println(element.getText());
}

問題在於您的 XPath。 這是產品名稱文本的 XPath。 這將為您提供具有產品名稱的所有元素。 然后只有您可以使用“getText()”方法。

//div[@class='MuiPaper-root MuiCard-root sc-Axmtr hvJMgY jss413 MuiPaper-elevation0 MuiPaper-rounded']/div/p[1]

使用此技術獲取您想要的所有文本。

暫無
暫無

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

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