无法调用“ org.openqa.selenium.webelement.click()”因为“ this.hotelslink”是无效的

发布于 2025-01-24 08:44:49 字数 2742 浏览 0 评论 0原文

我尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2025-01-31 08:44:49

我可以看到以下问题:

  1. 看来您没有将驱动程序实例传递给主页?

  2. 不需要此行 - system.setproperty(“ webdriver.chrome.driver”,“ chromedriver”);

  3. hotelslink定位器似乎不正确。

     软件包pageObject;
    
    导入net.serenitybdd.core.annotations.findby.findby;
    导入net.thucydides.core.annotations.defaulturl;
    导入net.thucydides.core.pages.pageObject;
    导入org.openqa.selenium.Webelement;
    
    @defaulturl(“页面:主页”)
    公共类主页扩展了PageObject {
       //菜单
     @findby(xPath =“ //*[@ID = \“ Fadein \”] ”)
     WebElement homelinks;
     @findby(xpath =“ // span [包含(。,'hotels')]]”)
     WebElement Hotelslink;
     @findby(xpath =“ // a [@title ='travelstart'] // span [text()='flaghts']”)
     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 ='upert'] // span [text()='umplor']”)
     webElement提供链接;
    
     @findby(xpath =“ // a [@title ='ivisa'] // span [text()='c']”)
     WebElement Visalink;
    
    公共主页(WebDriver驱动程序){
        超级(驱动程序); //如果要将驱动程序实例传递给超级类
        pageFactory.initements(驱动程序,this);
    }
    
    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();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://phptravels.net/");

       Homepage homepage = new Homepage(driver);
       homepage.checkLink();
    }
}

执行结果:
我进行了上述更改,对我来说效果很好。
(注意:我已经删除了PageObjects类和一些宁静的导入,因为您没有共享)

I could see the following issues:

  1. It seems you didn't pass driver instance to HomePage?

  2. This line is not needed - System.setProperty("webdriver.chrome.driver","chromedriver");

  3. The hotelsLink locator seems incorrect.

    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="//span[contains(.,'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 Homepage(WebDriver driver) {
        super(driver);//In case you want to pass the driver instance to super class
        PageFactory.initElements(driver,this);
    }
    
    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();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://phptravels.net/");

       Homepage homepage = new Homepage(driver);
       homepage.checkLink();
    }
}

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)
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文