How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium

huangapple go评论78阅读模式
英文:

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' 的定位时遇到了困难。

步骤:

  1. 使用用户ID:admin,密码:admin123登录到上述网站
  2. 点击 'Admin' > 'User Management'
  3. 点击 'Add user'
  4. 我想从 '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:

  1. login to the above siteusing user id:admin,pwd:admin123
  2. click on 'Admin' >'User Management'
  3. click on 'Add user"
  4. 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)

结果:

How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium

<h2>更新:</h2> 点击<kbd>F12</kbd>键,然后点击用户角色下拉框。您会注意到,&lt;div role=&quot;listbox&quot;&gt;元素变得可见(请参见下面的屏幕截图)。

How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium

英文:

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=&#39;listbox&#39;]). Using this element, you can easily locate its child elements(which basically will be the dropdown values).

  • (//div[@role=&#39;listbox&#39;]//child::div)[3] -- This is to select ESS
  • (//div[@role=&#39;listbox&#39;]//child::div)[2] -- This is to select Admin
# Click on dropdown downarrow element
driver.find_element(By.XPATH, &quot;(//i[@class=&#39;oxd-icon bi-caret-down-fill oxd-select-text--arrow&#39;])[1]&quot;).click()
# Click on ESS element
driver.find_element(By.XPATH, &quot;(//div[@role=&#39;listbox&#39;]//child::div)[3]&quot;).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(&quot;https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser&quot;)
driver.maximize_window()
driver.implicitly_wait(30)

# below 3 lines will log into the application
driver.find_element(By.NAME, &quot;username&quot;).send_keys(&quot;Admin&quot;)
driver.find_element(By.NAME, &quot;password&quot;).send_keys(&quot;admin123&quot;)
driver.find_element(By.XPATH, &quot;//button[@type=&#39;submit&#39;]&quot;).click()

# click on Admin menu
driver.find_element(By.XPATH, &quot;//span[text()=&#39;Admin&#39;]&quot;).click()
# Click on Add user
driver.find_element(By.XPATH, &quot;//button[contains(.,&#39;Add&#39;)]&quot;).click()
# Click on dropdown downarrow element
driver.find_element(By.XPATH, &quot;(//i[@class=&#39;oxd-icon bi-caret-down-fill oxd-select-text--arrow&#39;])[1]&quot;).click()
# Click on ESS element
driver.find_element(By.XPATH, &quot;(//div[@role=&#39;listbox&#39;]//child::div)[3]&quot;).click()
time.sleep(10)

RESULT:

How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium

<h2>UPDATE:</h2> Click <kbd>F12</kbd> key, and click on the User Role dropdown. You will notice, &lt;div role=&quot;listbox&quot;&gt; element getting visible(see below screen).

How to locate the webelement 'User Role' under admin menu in opensource-demo.orangehrmlive.com using Selenium

huangapple
  • 本文由 发表于 2023年6月30日 01:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定