简体   繁体   中英

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

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 :

  • Using 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 :

  • Using 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:

[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 .

And the div.MuiCardContent-root.jss414 selector looks better:

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. Here is the XPath for the product name text. This will give you all the elements that have the product name. Then only you can use the 'getText()' method.

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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