簡體   English   中英

Selenium,Java - 無法定位元素

[英]Selenium, Java - Unable to locate element

我是測試自動化的初學者。 我想自動化一個演示網站,但是我無法找到元素我的帳戶 -> 登錄 URL: https ://phptravels.net/home 我試圖通過 CSSseletor、Xpath 等找到它,但我總是收到錯誤-org.openqa.selenium.NoSuchElementException

public class LandingPage {

    public WebDriver driver;

    By myAccount = By.className("dropdown dropdown-login dropdown-tab");
    By login = By.xpath("//a[@class='dropdown-item active tr']");
    By navBar = By.id("mobileMenuMain");


    public LandingPage(WebDriver driver) {
        this.driver = driver;
    }

    public WebElement getMyAccount() {
        return driver.findElement(myAccount);
    }
    public WebElement getNavigation() {
        return driver.findElement(navBar);
    }

    public WebElement getLogin() {
        return driver.findElement(login);

-------------------------


@RunWith(Cucumber.class)
public class stepDefinition extends Base {

    @Given("^Initialize the browser with Chrome$")
    public void initialize_the_browser_with_chrome() throws Throwable {
        driver = initializeDriver();
    }

    @And("^Navigate to the \"([^\"]*)\" Website$")
    public void navigate_to_the_something_website(String strArg1) throws Throwable {
        driver.get(strArg1);

    }

   @And("^Click on My Account link in Home Page to land on sign in page$")
    public void click_on_My_Account_link_in_home_page_to_land_on_sign_in_page() throws Throwable {

       LandingPage landingPage = new LandingPage(driver);
       landingPage.getMyAccount().click();
       landingPage.getLogin().click();

       /*if (landingPage.getPopUpSize() > 0) {
           landingPage.getPopUp().click();
       }*/

   }
    @When("^User enters (.+) and (.+) and logs in$")
    public void user_enters_and_and_logs_in(String email, String password) throws Throwable {
        LoginPage loginPage = new LoginPage(driver);
        loginPage.getEmail().sendKeys(email);
        loginPage.getPassword().sendKeys(password);
        loginPage.getLoginbtn().click();
    }

    @Then("^Verify that user is successfully logged in$")
    public void verify_that_user_is_successfully_logged_in() throws Throwable {
        PortalHomePage p = new PortalHomePage(driver);
        Assert.assertTrue(p.getDemoUser().isDisplayed());
    }

    @And("^Close the browsers$")
    public void close_the_browsers() throws Throwable {
        driver.quit();
    }
}

有人可以幫忙嗎?

通過 Id 你會遇到一個問題,因為 id dropdownCurrency在頁面上使用了兩次並且按類搜索它應該是.dropdown.dropdown-login.dropdown-tab

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM