2016-03-03 93 views
1

我正在運行以下代碼,並顯示「搜索框無法解析」的錯誤,如何解決此問題?請幫助。我無法找到我獲取此內容的原因錯誤感謝提前:)「searchbox」變量無法解析錯誤

import java.io.File; 
import java.io.FileInputStream; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class DataDriven_Ex { 

    public static void main(String[] args) { 

    FirefoxDriver driver = new FirefoxDriver(); 
    driver.get("http://www.amazon.in"); 

    WebElement searchbox = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")); 
    } 

    @Test 
    public void test() throws Exception { 

     File file = new File("F://Selenium excel//DataDriven.xlsx"); 
     FileInputStream fs = new FileInputStream(file); 
     XSSFWorkbook wb = new XSSFWorkbook(fs); 
     XSSFSheet sh = wb.getSheet("Sheet1"); 


     int rowcount = sh.getLastRowNum(); 

     for (int i = 0; i<=rowcount;i++) 
     { 

      String keyword = sh.getRow(i).getCell(0).getStringCellValue(); 


      searchbox.sendKeys(keyword); 

      searchbox.submit();  

     } 

    }  

    } 

回答

0

聲明

static WebElement searchbox; 

之前主要方法 - 如下面

static WebElement searchbox; 
public static void main(String[] args) { 

FirefoxDriver driver = new FirefoxDriver(); 
driver.get("http://www.amazon.in"); 

searchbox = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")); 
} 
+0

謝謝....代碼現在工作:) – Priyanka

0

這是因爲您的變量searchBox未在您的方法test()的上下文中聲明。

WebElement searchbox = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")); 

下到你的test()方法:

public void test() throws Exception { 
    //Move here.. 
    WebElement searchbox = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")); 

    File file = new File("F://Selenium excel//DataDriven.xlsx"); 
    FileInputStream fs = new FileInputStream(file); 
    XSSFWorkbook wb = new XSSFWorkbook(fs); 
    XSSFSheet sh = wb.getSheet("Sheet1"); 

    //Your other codes here.. 
} 

而如果你只是在main()宣佈,它不能從test()

移動這行代碼可見你可以讓searchBox變一個static變量,但我不建議那。一旦你完成static,變量將成爲class的一個屬性,而不再是單個對象。

+0

好吧.....只是想知道是什麼使它成爲一個靜態變量 – Priyanka

+0

@Priyanka的缺點,是不是缺點,這是關於相關性。只有當它是一個共享屬性時,我們纔將它設置爲靜態。將其設置爲靜態意味着您只有一個「複製」它,並在所有對象之間共享。這樣做時你必須小心謹慎,因爲改變它會影響所有其他對象。 – user3437460

+0

確定了它.....謝謝你的澄清 – Priyanka

1

您的搜索框變量是,在本地聲明爲靜態方法main()。搜索框在方法test()中不可見。這是代碼中的問題。

解決方案:將searchbox聲明從方法移至類。並將靜態修飾符添加到變量聲明中。請參閱下面的代碼。

static WebElement searchbox; 

public static void main(String[] args) 
{ 

    FirefoxDriver driver = new FirefoxDriver(); 
    driver.get("http://www.amazon.in"); 

    searchbox = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")); 
} 
+0

感謝您的詳細信息:) – Priyanka

+0

@Priyanka歡迎:) – Willmore

1

searchbox對象是本地的主要方法,你不能訪問本地變量出方的塊。這就是爲什麼你有這個問題。

解決方案:searchbox移到外部main,使其成爲一個類級別的靜態變量,然後訪問它。

public class DataDriven_Ex { 
    static WebElement searchbox; 

    public static void main(String[] args) { 

     FirefoxDriver driver = new FirefoxDriver(); 
     driver.get("http://www.amazon.in"); 
     searchbox = driver.findElement(By 
       .xpath("//*[@id='twotabsearchtextbox']")); 

    } 

    @Test 
    public void test() throws Exception { 

     File file = new File("F://Selenium excel//DataDriven.xlsx"); 
     FileInputStream fs = new FileInputStream(file); 
     XSSFWorkbook wb = new XSSFWorkbook(fs); 
     XSSFSheet sh = wb.getSheet("Sheet1"); 

     int rowcount = sh.getLastRowNum(); 

     for (int i = 0; i <= rowcount; i++) { 

      String keyword = sh.getRow(i).getCell(0).getStringCellValue(); 

      searchbox.sendKeys(keyword); 

      searchbox.submit(); 

     } 

    } 

} 
+1

好的解決方案確實 – Willmore

+0

:) @Willmore謝謝 –

+0

它現在的工作....謝謝 – Priyanka