Java &引用;搜索框“;无法解析变量错误
我正在运行以下代码,并收到一个错误,提示“searchbox无法解决。我如何解决此问题?请帮助。我无法找到出现此错误的原因。提前感谢:)Java &引用;搜索框“;无法解析变量错误,java,Java,我正在运行以下代码,并收到一个错误,提示“searchbox无法解决。我如何解决此问题?请帮助。我无法找到出现此错误的原因。提前感谢:) 导入java.io.File; 导入java.io.FileInputStream; 导入org.apache.poi.xssf.usermodel.xssfheet; 导入org.apache.poi.xssf.usermodel.xssf工作簿; 导入org.junit.Test; 导入org.openqa.selenium.By; 导入org.openq
导入java.io.File;
导入java.io.FileInputStream;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.junit.Test;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类数据驱动{
公共静态void main(字符串[]args){
FirefoxDriver=新的FirefoxDriver();
驱动程序。获取(“http://www.amazon.in");
WebElement searchbox=driver.findElement(By.xpath(“/*[@id='twotabsearchtextbox']);
}
@试验
public void test()引发异常{
File File=新文件(“F://Selenium excel//DataDriven.xlsx”);
FileInputStream fs=新的FileInputStream(文件);
XSSF工作簿wb=新XSSF工作簿(fs);
XSSFSheet sh=wb.getSheet(“Sheet1”);
int rowcount=sh.getLastRowNum();
对于(int i=0;i
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']"));
}
这是因为变量searchBox
没有在方法test()
的上下文中声明。如果您只是在main()
中声明它,则无法从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..
}
虽然您可以将searchBox
设置为static
变量,但我不建议这样做。一旦设置为static
,该变量将成为类的一个属性,而不再是单个对象。您的searchBox变量是在本地向静态方法声明的main()。从方法测试()看不到搜索框。这是代码中的问题
解决方案:将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']"));
}
您的searchbox
对象是main方法的本地对象,您无法在其块外访问本地变量。这就是您遇到此问题的原因
解决方案:将搜索框移动到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();
}
}
}
公共类数据驱动{
静态WebElement搜索框;
公共静态void main(字符串[]args){
FirefoxDriver=新的FirefoxDriver();
驱动程序。获取(“http://www.amazon.in");
searchbox=驱动程序.findElement(按
.xpath(“/*[@id='twotabsearchtextbox']”);
}
@试验
public void test()引发异常{
File File=新文件(“F://Selenium excel//DataDriven.xlsx”);
FileInputStream fs=新的FileInputStream(文件);
XSSF工作簿wb=新XSSF工作簿(fs);
XSSFSheet sh=wb.getSheet(“Sheet1”);
int rowcount=sh.getLastRowNum();
对于(inti=0;我感谢您提供的详细信息:)好的……我只是想知道将其设置为静态的缺点是什么variable@Priyanka这不是缺点,而是相关性。只有当它是共享属性时,我们才将其设置为静态。将其设置为静态意味着您只有一个“副本”“是的,它在所有对象之间共享。这样做时必须小心,因为它的更改将影响所有其他对象。
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();
}
}
}