gpt4 book ai didi

java - Selenium FindBy Amazon 搜索给出错误。 (java.lang.NullPointerException)

转载 作者:行者123 更新时间:2023-12-02 08:49:53 26 4
gpt4 key购买 nike

在搜索类中,我创建了一个进行亚马逊搜索的方法,主类调用方法 searchFor()但我不断收到错误

线程“main”中的异常 java.lang.NullPointerException

package Project1;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class Search {

@FindBy(id = "twotabsearchtextbox")
WebElement search_box;


public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}



}

这是主类

package Project1;


public class Main {

public static void main(String[] args) {

Search s1 = new Search();

s1.searchFor("gaming laptop");

}
}

最佳答案

请引用以下解决方案:

主类

public class Main {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("Your url ");

Search s1 = new Search(driver);
s1.searchFor("gaming laptop");


}
}

搜索类别

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Search {

@FindBy(id = "twotabsearchtextbox")
WebElement search_box;

WebDriver driver;
public Search(WebDriver driver){

this.driver = driver;
PageFactory.initElements(driver, this);

}
public void searchFor(String content) {

search_box.sendKeys(content);
search_box.submit();
}

}

关于java - Selenium FindBy Amazon 搜索给出错误。 (java.lang.NullPointerException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60848810/

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