无法调用“ org.openqa.selenium.webelement.click()”因为“ this.hotelslink”是无效的
我尝试使用Selenium进行一些测试,但是我无法测试我做的
homepage.java
package PageObject;
import net.serenitybdd.core.annotations.findby.FindBy;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.WebElement;
@DefaultUrl("page:homepage")
public class Homepage extends PageObject {
//Menu
@FindBy(xpath = "//*[@id=\"fadein\"]/header/div[2]/div/div/div/div/div[2]/nav/ul/li[1]/a")
WebElement homeLInks;
@FindBy(xpath="//a[@title='Hotels']//span[text()='Hotels ']")
WebElement hotelsLink;
@FindBy(xpath="//a[@title='Travelstart']//span[text()='Flights ']")
WebElement flightsLink;
@FindBy(xpath="//a[@title='Tours']//span[text()='Tours ']")
WebElement toursLink;
@FindBy(xpath="//a[@title='Cars']//span[text()='Cars ']")
WebElement carsLink;
@FindBy(xpath="//a[@title='Offers']//span[text()='Offers ']")
WebElement offersLink;
@FindBy(xpath="//a[@title='Ivisa']//span[text()='c']")
WebElement visaLink;
public void checkLink(){
hotelsLink.click();
}
}
hooks.java
import PageObject.Homepage;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Hooks {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
System.setProperty("webdriver.chrome.driver","chromedriver");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://phptravels.net/");
Homepage homepage = new Homepage();
homepage.checkLink();
}
}
错误:
slf4j:无法加载类“ org.slf4j.impl.staticloggerbinder”。 SLF4J:默认为无操作(NOP)Logger实现 slf4j:请参阅 http:///www.slf4j.org/codes.htmlugginderbinderbinderbinderbinderbinderbinderbinderbinderbinderbinderbinder https://chromedriver.chromium.chromium.org/security-comentations 安全的。 Chromedriver成功开始了。 2022年4月27日2:46:27 pm org.openqa.selenium.remote.protocolhandshake创建 信息:检测到的方言:W3C 2022年4月27日2:46:27 pm org.openqa.selenium.devtools.cdpversionfinder findNeareStMatch 警告:找不到CDP版本101的完全匹配,因此返回最接近的版本:99 2022年4月27日2:46:27 pm org.openqa.selenium.devtools.cdpversionfinder findNeareStMatch 信息:发现了99版101版的CDP实现 线程“ main” java.lang.nullpoInterException:nuck.openqa.selenium.webelement.click()中的异常 在pageObject.homepage.checkcookie(homepage.java:73) 在胡克斯(Hooks.main)(hooks.java:19)
I try to do some tests using Selenium, but I can't test what I do
HomePage.java
package PageObject;
import net.serenitybdd.core.annotations.findby.FindBy;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.WebElement;
@DefaultUrl("page:homepage")
public class Homepage extends PageObject {
//Menu
@FindBy(xpath = "//*[@id=\"fadein\"]/header/div[2]/div/div/div/div/div[2]/nav/ul/li[1]/a")
WebElement homeLInks;
@FindBy(xpath="//a[@title='Hotels']//span[text()='Hotels ']")
WebElement hotelsLink;
@FindBy(xpath="//a[@title='Travelstart']//span[text()='Flights ']")
WebElement flightsLink;
@FindBy(xpath="//a[@title='Tours']//span[text()='Tours ']")
WebElement toursLink;
@FindBy(xpath="//a[@title='Cars']//span[text()='Cars ']")
WebElement carsLink;
@FindBy(xpath="//a[@title='Offers']//span[text()='Offers ']")
WebElement offersLink;
@FindBy(xpath="//a[@title='Ivisa']//span[text()='c']")
WebElement visaLink;
public void checkLink(){
hotelsLink.click();
}
}
Hooks.java
import PageObject.Homepage;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Hooks {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
System.setProperty("webdriver.chrome.driver","chromedriver");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://phptravels.net/");
Homepage homepage = new Homepage();
homepage.checkLink();
}
}
Error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 38072
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Apr 27, 2022 2:46:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Apr 27, 2022 2:46:27 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 101, so returning the closest version found: 99
Apr 27, 2022 2:46:27 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found CDP implementation for version 101 of 99
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.click()" because "this.hotelsLink" is null
at PageObject.Homepage.checkCookie(Homepage.java:73)
at Hooks.main(Hooks.java:19)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看到以下问题:
看来您没有将驱动程序实例传递给主页?
不需要此行 - system.setproperty(“ webdriver.chrome.driver”,“ chromedriver”);
hotelslink定位器似乎不正确。
}
hooks.java
执行结果:
我进行了上述更改,对我来说效果很好。
(注意:我已经删除了PageObjects类和一些宁静的导入,因为您没有共享)
I could see the following issues:
It seems you didn't pass driver instance to HomePage?
This line is not needed - System.setProperty("webdriver.chrome.driver","chromedriver");
The hotelsLink locator seems incorrect.
}
Hooks.java
Execution Result:
I made the above changes and its working fine for me.
(Note: I have removed PageObjects class and some Serenity imports as you didn't share them)