Java 如何在控制台中打印具有相同类名(人类可读)的元素/值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37385541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:15:07  来源:igfitidea点击:

How to print elements/values in console which have same class name (human-readable)?

javaselenium-webdriver

提问by Saba Malik

I have used this:

我用过这个:

String GDP = driver.findElement(By.className("human-readable")).getText(); 
System.out.println(GDP); 

...to print one. What should I do for other one?

...打印一个。我该怎么办?

回答by Rudy

You should use a CSS selector instead. Below is the sample of cssSelector using Id. For the class I guess you can use [class='human-readable'].

您应该改用 CSS 选择器。下面是使用 Id 的 cssSelector 示例。对于课程,我想您可以使用 [class='human-readable']。

String cssSelRefreshBtn="[id='refreshBtn']"; 
List<WebElement> a=driver.findElements(By.cssSelector(cssSelRefreshBtn)) ;
for(WebElement item:a)
{
    String b= item.getText();
    System.out.println(b);
}

回答by Naman

Try using List <WebElement>to access all similar elements :

尝试使用List <WebElement>访问所有类似元素:

List<WebElement> listElement = driver.findElements(By.className("human-readable"));
for(int i =0;i<listElement.size();i++) {
 String elementText = listElement.get(i).getText(); 
 System.out.println(elementText); 
}

回答by Viren

Please use below:

请在下面使用:

Point p = driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span[1]")).getLocation();
int xCord = p.getX();
int yCord = p.getY();
System.out.println("The Position of UserName is "+ xCord +","+yCord +" pixels");