Selenium FindBy Amazon 搜索给出错误。 (java.lang.NullPointerException)
Selenium FindBy Amazon Search Giving Error. (java.lang.NullPointerException)
有一个搜索 Class,我在其中创建了一个方法来进行亚马逊搜索,而 Main Class 调用了方法 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();
}
}
这是主要的 Class
package Project1;
public class Main {
public static void main(String[] args) {
Search s1 = new Search();
s1.searchFor("gaming laptop");
}
}
请参考以下解决方案:
主要class
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");
}
}
搜索class
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();
}
}
有一个搜索 Class,我在其中创建了一个方法来进行亚马逊搜索,而 Main Class 调用了方法 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();
}
}
这是主要的 Class
package Project1;
public class Main {
public static void main(String[] args) {
Search s1 = new Search();
s1.searchFor("gaming laptop");
}
}
请参考以下解决方案:
主要class
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");
}
}
搜索class
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();
}
}