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
在页面上使用了两次,按 class 搜索它应该是 .dropdown.dropdown-login.dropdown-tab
我是测试自动化的初学者。 我想自动化一个演示网站,但是我无法找到我的帐户 -> 登录元素 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
在页面上使用了两次,按 class 搜索它应该是 .dropdown.dropdown-login.dropdown-tab