问题
Steps:
- Go to the site > https://www.toolsqa.com/automation-practice-switch-windows/
- Get a list of buttons from that page
- Print the name of the buttons that are displayed on the page.
Code trial:
package com.practice;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Buttons {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Oderint dum metuant\\eclipse-workspace\\JAR FILES\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.toolsqa.com/automation-practice-switch-windows/");
List <WebElement> buttons = driver.findElements(By.tagName("button"));
for ( int i=0; i<buttons.size();i++){
WebElement button = buttons.get(i);
if(button.isEnabled()){
System.out.println(buttons);
}}}}
回答1:
To print all the button texts within the url you can use Java8's stream()
and map()
and you can use the following solution:
Line of code:
System.out.println(driver.findElements(By.tagName("button")).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
Console Output:
[New Browser Window, New Message Window, New Browser Tab, Alert Box, Timing Alert, Change Color, Change Color, Disabled, Visible, , , , , , ]
回答2:
Instead of using By.tagName method I used By.cssSelector method
here is the Working code...
package stackOverflow;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ToolsqaCom {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Tushar\\JARs\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.toolsqa.com/automation-practice-switch-windows");
driver.manage().window().maximize();
// ArrayList<String> l1 = new ArrayList<String>();
// WebElement b1 = driver.findElement(By.id("button1"));
// l1.add(b1.getText());
java.util.List<WebElement> b2 = driver.findElements(By.cssSelector("p button"));
for (int i = 0; i < b2.size() - 1; i++) {
String string = b2.get(i).getText();
System.out.println(string);
}}}
Following is the Output:
New Browser Window
New Message Window
New Browser Tab
Alert Box
Timing Alert
Change Color
Change Color
Disabled
回答3:
- If you want to print the list of button names that are displayed on the page.
Ans.Remove if condition block and change the print statement. Refer below Code:
List <WebElement> buttons = driver.findElements(By.tagName("button"));
for ( int i=0; i<buttons.size();i++)
{
WebElement button = buttons.get(i);
System.out.println(button.getText());//It prints all the buttons name displayed on the page
}
- If you want to print the list of button names which are enabled on the page.
A. Keep the if condition block, just change the print statement. Refer below code:
List <WebElement> buttons = driver.findElements(By.tagName("button"));
for ( int i=0; i<buttons.size();i++)
{
WebElement button = buttons.get(i);
//System.out.println(button.getText()); //It prints all the buttons name displayed on the page
if(button.isEnabled())
{
System.out.println(button.getText()); //It prints all the buttons name which are enabled on the page
}
}
来源:https://stackoverflow.com/questions/56388392/how-to-print-all-the-button-texts-within-the-url-using-selenium-through-java