英文:
How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium
问题
我在查找网站 https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser 内的网页元素 'User Role' 的定位时遇到了困难。
步骤:
- 使用用户ID:admin,密码:admin123登录到上述网站
- 点击 'Admin' > 'User Management'
- 点击 'Add user'
- 我想从 'User Role' 下拉菜单中选择 'ESS'
但我无法找到 ESS 网页元素的定位。
请帮我解决。
英文:
I find difficult in finding the locator for the webelement 'User Role' within the website: https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser
Steps:
- login to the above siteusing user id:admin,pwd:admin123
- click on 'Admin' >'User Management'
- click on 'Add user"
- I want to choose 'ESS' from 'User Role' drop down
But im not able to find the locator for ESS webelement.
Pls help me out.
答案1
得分: 1
在网站 https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser 中定位 User Role 的 webelement,您可以使用以下定位策略:
-
XPATH:
//label[contains(., 'User Role')]//following::div[1]//div[@class='oxd-select-text-input']
英文:
To locate the webelement for User Role within the website https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser you can use the following locator strategy:
-
XPATH:
//label[contains(., 'User Role')]//following::div[1]//div[@class='oxd-select-text-input']
答案2
得分: 1
Here is the translated content:
问题: 下拉框的值是消失的元素,这意味着您不能检查元素并轻松定位所需的元素。
解决方案: 请参考以下代码。您需要首先点击下拉箭头元素,以使下拉框的值可见。然后定位这些下拉框值的父元素(//div[@role='listbox']
)。使用这个元素,您可以轻松定位其子元素(基本上就是下拉框的值)。
(//div[@role='listbox']//child::div)[3]
-- 这是选择ESS
的代码。(//div[@role='listbox']//child::div)[2]
-- 这是选择Admin
的代码。
# 点击下拉箭头元素
driver.find_element(By.XPATH, "(//i[@class='oxd-icon bi-caret-down-fill oxd-select-text--arrow'])[1]").click()
# 点击ESS元素
driver.find_element(By.XPATH, "(//div[@role='listbox']//child::div)[3]").click()
检查完整工作代码和解释如下:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser")
driver.maximize_window()
driver.implicitly_wait(30)
# 下面的3行将登录到应用程序
driver.find_element(By.NAME, "username").send_keys("Admin")
driver.find_element(By.NAME, "password").send_keys("admin123")
driver.find_element(By.XPATH, "//button[@type='submit']").click()
# 点击Admin菜单
driver.find_element(By.XPATH, "//span[text()='Admin']").click()
# 点击添加用户
driver.find_element(By.XPATH, "//button[contains(.,'Add')]").click()
# 点击下拉箭头元素
driver.find_element(By.XPATH, "(//i[@class='oxd-icon bi-caret-down-fill oxd-select-text--arrow'])[1]").click()
# 点击ESS元素
driver.find_element(By.XPATH, "(//div[@role='listbox']//child::div)[3]").click()
time.sleep(10)
结果:
<h2>更新:</h2> 点击<kbd>F12</kbd>键,然后点击用户角色
下拉框。您会注意到,<div role="listbox">
元素变得可见(请参见下面的屏幕截图)。
英文:
Problem: The dropdown values are disappearing elements, which means you cannot inspect the elements and locate the desired element easily.
Solution: Refer below code. You need to click on the dropdown down arrow element first so that dropdown values are visible. Then locate the parent element of these dropdown values (//div[@role='listbox']
). Using this element, you can easily locate its child elements(which basically will be the dropdown values).
(//div[@role='listbox']//child::div)[3]
-- This is to selectESS
(//div[@role='listbox']//child::div)[2]
-- This is to selectAdmin
# Click on dropdown downarrow element
driver.find_element(By.XPATH, "(//i[@class='oxd-icon bi-caret-down-fill oxd-select-text--arrow'])[1]").click()
# Click on ESS element
driver.find_element(By.XPATH, "(//div[@role='listbox']//child::div)[3]").click()
Check the complete working code and explanation below:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser")
driver.maximize_window()
driver.implicitly_wait(30)
# below 3 lines will log into the application
driver.find_element(By.NAME, "username").send_keys("Admin")
driver.find_element(By.NAME, "password").send_keys("admin123")
driver.find_element(By.XPATH, "//button[@type='submit']").click()
# click on Admin menu
driver.find_element(By.XPATH, "//span[text()='Admin']").click()
# Click on Add user
driver.find_element(By.XPATH, "//button[contains(.,'Add')]").click()
# Click on dropdown downarrow element
driver.find_element(By.XPATH, "(//i[@class='oxd-icon bi-caret-down-fill oxd-select-text--arrow'])[1]").click()
# Click on ESS element
driver.find_element(By.XPATH, "(//div[@role='listbox']//child::div)[3]").click()
time.sleep(10)
RESULT:
<h2>UPDATE:</h2> Click <kbd>F12</kbd> key, and click on the User Role
dropdown. You will notice, <div role="listbox">
element getting visible(see below screen).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论