【问题标题】:get (int) method undefined for the type listget (int) 方法未定义类型列表
【发布时间】:2018-07-10 07:10:03
【问题描述】:

我正在尝试使用未定义的 get(int) 方法为类型列表创建一个 selenium 脚本,如果未选择,该类型列表将选择一个单选按钮。

我使用以下脚本:

package automationFramework;

import java.awt.List;

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

public class FirstTestCase {

    public static void main(String[] args) throws InterruptedException {
        // Create a new instance of the Firefox driver
        System.setProperty("webdriver.gecko.driver", "/home/gradulescu/Documents/Eclipse project/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

        // Storing the Application Url in the String variable
        String url = "http://toolsqa.wpengine.com/automation-practice-form/";
        driver.get(url);        
        //Launch the Online Store Website
        List Rbtn = (List) driver.findElement(By.name("sex"));
        boolean bool = false;
        bool =  Rbtn.get(0).isSelected();
        if (bool==true)
        {Rbtn.get(1).click();
            }
        else
        {Rbtn.get(0).click();
            }

    }
}

get方法返回如下错误:

类型列表的方法 get(int) 未定义

我使用带有 JConsole 1.8.0_171-b11 的 Eclipse 3.8.1 和 Java VM 版本:Java HotSpot(TM) 64-Bit Server VM, 25.171-b11

你能帮帮我吗?

【问题讨论】:

    标签: java selenium get


    【解决方案1】:

    正如 WebDriver 的文档所述,findElements(By by) 方法返回 java.util.List<WebElement>WebElement 类的对象列表)而不是 java.awt.List列表awt中的UI组件)。

    您需要更改您的导入,并将代码更改为:

    List<WebElement> rbtn = driver.findElement(By.name("sex"));
    

    注意:在 Java 中,变量、参数、方法名称必须以小写字母开头

    【讨论】:

      【解决方案2】:

      替换:

      List Rbtn = (List) driver.findElement(By.name("sex"));
      

      与:

      List<WebElement> rBtn = driver.findElements(By.name("sex")); // this will return a list of all found elements
      

      这是一个可行的示例代码:

      driver.get("http://toolsqa.wpengine.com/automation-practice-form/");
      
      List<WebElement> rBtn = driver.findElements(By.name("sex")); // this will return a list of all found elements
      if (rBtn.get(1).isSelected()) // I'm getting the second element because the first one is only label
      {   
         rBtn.get(2).click(); //click 'Female'
      }
      else
      {   
         rBtn.get(1).click(); //click 'Male'
      }
      

      PS:始终在开发工具中检查您使用选择器定位的元素。要打开开发工具,请在浏览器中按F12。更多信息here.

      【讨论】:

        【解决方案3】:

        boolean 默认为 false,因此您不必显式初始化它。同样,if(bool) 也可以。第三,将它们存储在列表中时需要使用findElements。第四,使用try...catch 块是一个好习惯。

        试试-

        package automationFramework;
        
        import java.awt.List;
        
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.firefox.FirefoxDriver;
        
        public class FirstTestCase {
        
            public static void main(String[] args){
                try {
                    System.setProperty("webdriver.gecko.driver", "/home/gradulescu/Documents/Eclipse project/geckodriver.exe");
                    WebDriver driver = new FirefoxDriver();
        
                    // Storing the Application Url in the String variable
                    String url = "http://toolsqa.wpengine.com/automation-practice-form/";
                    driver.get(url);
                    // Launch the Online Store Website
                    List<WebElement> Rbtn = driver.findElements(By.name("sex"));
                    boolean bool;
                    bool = Rbtn.get(0).isSelected();
                    if (bool)
                        Rbtn.get(1).click();
                    else
                        Rbtn.get(0).click();
        
                } catch (Exception e) {
                    e.printStackTrace();
                }
        
            }
        }
        

        【讨论】:

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