【问题标题】:How we click same class name ON button for different field name in selenium webdriver我们如何在 selenium webdriver 中为不同的字段名单击相同的类名 ON 按钮
【发布时间】:2016-01-18 18:02:55
【问题描述】:

我们如何在 selenium webdriver 中为不同的字段名点击相同的类名来打开/关闭按钮

(例如) 1) 电子邮件通知 - 一个元素 2) 系统费用 - 第二要素 3) 生日 - 第三个元素

它们具有相同的类名 - “toggle-group”。我们如何单击这三个按钮。

我们如何为此编写点击按钮操作 不像复选框选项

【问题讨论】:

  • 如果所有标签/元素的类名相同,则可以使用“xpath”。

标签: java selenium


【解决方案1】:

正如你在这个helpful article 中看到的,你可以使用很多方法:

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"))

【讨论】:

    【解决方案2】:

    你可以通过文字找到元素

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

    或者

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

    【讨论】:

      【解决方案3】:
      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
      
             }
      
          }
      
      }
      

      希望这会有所帮助..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多