简体   繁体   中英

How we click same class name ON button for different field name in selenium webdriver

How we click same class name for ON / OFF button for different field name in selenium webdriver

(eg) 1) Email Notification - one element 2) System fees - second element 3) Birthdate - third element

these are have same class name - "toggle-group".How we click these three button.

How we write click button action for this Not like checkbox option

You can find the elements by their text

driver.findElement(By.linkText("first")).click();

Or

driver.findElement(By.partialLinkText("first")).click();

As you can see in this very helpful article , you can use many methods:

driver.findElement(By.id("element id"))

driver.findElement(By.className("element class"))

driver.findElement(By.name("element name"))

driver.findElement(By.tagName("element html tag name"))

driver.findElement(By.cssSelector("css selector"))

driver.findElement(By.link("link text"))

driver.findElement(By.xpath("xpath expression"))
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class check {

    public static void main(String[] args) {

       WebDriver driver = new FirefoxDriver();
       List<WebElement> we = new ArrayList<WebElement>();
       we = driver.findElements(By.name("chk"));
           we.get(0).click(); // clicks on "first"
           we.get(1).click(); // clicks on "second"
           we.get(2).click(); // clicks on "third"

     }
}


/* another option */


import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class check {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
       List<WebElement> we = new ArrayList<WebElement>();
       we = driver.findElements(By.name("chk"));

       for(WebElement check: we)
       {
           check.click(); // click all the 3 elements and comes out of loop

       }

    }

}

Hope this helps..

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