我正在以面向对象的方式创建测试,并将我的代码划分为单独的类。
如果我按程序编写代码,则效果很好。但是我想知道为什么当我将它分为多个类时,为什么找不到元素。
这是我按程序编写的代码,可以正常工作:
driver.get(common.WP_ADMIN);
WebElement user_name = driver.findElement(By.name("log"));
user_name.sendKeys(common.WP_USER);
WebElement password = driver.findElement(By.name("pwd"));
password.sendKeys(common.WP_PASS);
WebElement btn_submit = driver.findElement(By.name("wp-submit"));
btn_submit.submit();
driver.get(common.WP_ADMIN + "/customize.php");
//It can locate #adminmenu and it's clickable
WebElement site_identify = driver.findElement(By.cssSelector("#adminmenu"));
site_identify.click();
但是如果我将代码分成多个类 找不到“ #adminmenu”元素:
loginWP类:
driver.get(common.WP_ADMIN);
WebElement user_name = driver.findElement(By.name("log"));
user_name.sendKeys(common.WP_USER);
WebElement password = driver.findElement(By.name("pwd"));
password.sendKeys(common.WP_PASS);
WebElement btn_submit = driver.findElement(By.name("wp-submit"));
btn_submit.submit();
driver.get(common.WP_ADMIN + "/customize.php");
AdminMenu类:
public class AdminMenu {
public void adminMenuTest(WebDriver driver) {
//It can't be loacted
WebElement site_identify = driver.findElement(By.cssSelector("#adminmenu"));
}
}
主类:
//Login to Wordpress
login.loginWP(common.WP_ADMIN, common.WP_USER, common.WP_PASS);
// Begin test
AdminMenu adminMenu = new AdminMenu();
adminMenu.adminMenuTest(driver);
它向我展示了这一点:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#adminmenu"}
为什么在使用类时找不到元素?