如何使用硒在Web應用程序中執行菜單列表選擇?


5

我正在嘗試選擇/導航到提供的URL中的菜單項。我嘗試了"操作",但是沒有用。

https://enterprise-demo.orangehrmlive.com/auth/login

uid / pwd:管理員/管理員

我想選擇菜單導航(例如Admin-> User Management-> User Roles)

By admin = By.id("menu_admin_viewAdminModule");
By uMgmt = By.id("menu_admin_UserManagement");
By uRoles = By.id("menu_admin_viewUserRoles");
By username = By.id("searchSystemUser_userName");
By search = By.id("searchBtn");

WebDriver driver2 = driver;
Actions act = new Actions(driver2);

WebElement wadmin = driver2.findElement(admin);
WebElement wusermgmt = driver2.findElement(uMgmt);
WebElement wuserroles = driver2.findElement(uRoles);

act.moveToElement(wadmin).moveToElement(wusermgmt).moveToElement(wuserroles).click().build().perform();
4

You can use the Actions API for that, given that the browser you use supports it. The following should do the trick with Chrome:

  1. Moves the mouse over the "Admin" button
  2. Waits until the "User Management" button is visible
  3. Moves the mouse over the "User Management" button
  4. Waits until the "Users" button is visible
  5. Clicks the "Users" button:

Java

WebElement admin = driver.findElement(By.xpath("//b[contains(., 'Admin')]"));

new Actions(driver).moveToElement(admin).perform();

WebElement userManagement = new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("menu_admin_UserManagement")));
new Actions(driver).moveToElement(userManagement).perform();

WebElement users = new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("menu_admin_viewSystemUsers")));
users.click();

1

You can perform this by using linkText or partialLinkText:

// System.setProperty("webdriver.chrome.driver", "yourChromeLocation\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://enterprise-demo.orangehrmlive.com/");
driver.findElement(By.id("txtUsername")).sendKeys("Admin");
driver.findElement(By.id("txtPassword")).sendKeys("admin");
driver.findElement(By.id("btnLogin")).click();

WebElement admin = driver.findElement(By.xpath("//b[contains(., 'Admin')]"));

new Actions(driver).moveToElement(admin).perform();

WebElement userManagement = new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("menu_admin_UserManagement")));
new Actions(driver).moveToElement(userManagement).perform();
//driver.findElement(By.partialLinkText("User Roles")).click();

WebElement users = new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.linkText("User Roles")));
users.click();