gpt4 book ai didi

java - "searchbox"变量无法解析错误

转载 作者:行者123 更新时间:2023-11-30 03:01:49 25 4
gpt4 key购买 nike

我正在运行以下代码并收到错误消息“无法解析搜索框。我该如何解决此问题?请帮助。我无法找出收到此错误的原因。提前致谢:)

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();

}

}

}

最佳答案

您的搜索框变量在本地声明到静态方法main()。搜索框在 test() 方法中不可见。这是代码中的问题。

解决方案:将搜索框声明从方法移动到类。并在变量声明中添加 static 修饰符。请参阅下面的代码。

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']"));
}

关于java - "searchbox"变量无法解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com