簡體   English   中英

錯誤:無法在 selenium webdriver 中實例化 Select 類型

[英]Error :Cannot instantiate the type Select in selenium webdriver

我正在使用 selenium webdriver 測試網站。 我也導入了所有的 jar 文件,但仍然無法在 eclipse 中使用 Select Class。 它給了我一個錯誤: Select class cannot be instantiated. 我還導入了org.openqa.selenium.support.ui.Select

以下是我的源代碼

import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Dropdown {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.makemytrip.com");
          WebElementaddress=driver.findElement(By.xpath(".//[@id='to_typeahead1']"));
        Select sc = new Select (address);  // ERROR LINE
        sc.selectByIndex(5);
    }

}

你可以試試下面的。 讓我們把它分解成幾部分。

Select sc = new Select(driver.findElement(By.xpath("your Xpath match case"))); 
                                 **//this will get the dropdown into sc object**
List<WebElement> we = sc.getOptions();  **//to get the options values into list**
System.out.println(we.size());  **//to print the size in console, this and
                                  previous lines for debug/cross checking**
sc.selectByIndex(5);  **//this will select the 5th index and 6th value(indexing starts from 0)**

干杯!

Import org.openqa.selenium.support.ui.Select; package in your project, instead of org.apache.bcel.generic.Select;

錯誤將消失。

我看到您提供的 Xpath 不正確。 它應該是.//*[@id='to_typeahead1']

而且,這個元素不是一個選擇框。 它是一個輸入框。 您可能可以嘗試的是單擊元素,然后嘗試單擊要選擇的選項。

我使用了org.openqa.selenium.support.ui.Select包,它運行良好。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class NewClassTest {

    WebDriver driver = new FirefoxDriver();

    @Test
    public void selectOption() {
        driver.get("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");

        WebElement address = driver.findElement(By.tagName("select"));
        Select ab = new Select(address);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM