带有Java的WebDriver-无法使用Webdriver获得所有文本 [英] WebDriver with Java - Not able to get all the text using Webdriver

查看:37
本文介绍了带有Java的WebDriver-无法使用Webdriver获得所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在此网站上获得所有文本- https://niftygateway.com/marketplace

I'm not able to get all the text on this website - 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 ,您可以使用以下定位器策略:

To get all the text on this website https://niftygateway.com/marketplace you can use the following Locator Strategy:

  • 使用 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:

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

控制台输出:

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

这篇关于带有Java的WebDriver-无法使用Webdriver获得所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆