0

Steps:

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

3 Answers 3

1

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

0
  1. 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

    }
  1. 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

            }    
        }
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.