简体   繁体   English

带有 Java 的 WebDriver - 无法使用 Webdriver 获取所有文本

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

I'm not able to get all the text on this website - https://niftygateway.com/marketplace我无法获取此网站上的所有文本 - https://niftygateway.com/marketplace

Using this simple code -使用这个简单的代码 -

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());
        }

To get all the text on this website https://niftygateway.com/marketplace you can use the following Locator Strategy :要获取此网站https://niftygateway.com/marketplace上的所有文本,您可以使用以下定位器策略

  • Using cssSelector :使用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()));

Ideally, you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following Locator Strategy :理想情况下,您需要为visibilityOfAllElementsLocatedBy()引入WebDriverWait ,并且可以使用以下Locator Strategy

  • Using xpath :使用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()));

Console Output:控制台 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]

To wait for the list can use visibilityOfAllElementsLocatedBy .要等待列表可以使用visibilityOfAllElementsLocatedBy

And the div.MuiCardContent-root.jss414 selector looks better:并且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());
}

The issue is with your XPath.问题在于您的 XPath。 Here is the XPath for the product name text.这是产品名称文本的 XPath。 This will give you all the elements that have the product name.这将为您提供具有产品名称的所有元素。 Then only you can use the 'getText()' method.然后只有您可以使用“getText()”方法。

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

Use this technique to get all the texts that you want.使用此技术获取您想要的所有文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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