使用硒选择下拉选项

问题描述 投票:0回答:1

我正在尝试填写此表格,但无法真正选择下拉选项,链接 https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser

我也无法使用select,因为HTML代码中没有select标签

user_role = Select(driver.find_element(By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]"))
user_role.select_by_visible_text("Admin")

这显示了 select 不能用在 div 标签上的错误

python selenium-webdriver testing drop-down-menu ui-automation
1个回答
0
投票

下拉元素不是用

Select
标签构建的,所以
Select
库不能用于与下拉列表交互。

在尝试检查选项时,选项也会消失。参考如何检查浏览器中消失的元素?

下面是我可以点击

Admin
选项的代码。

# Imports for Explicit waits
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver,30)

driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")

# to navigate to the Add user page.
time.sleep(20)

# find all the elements for the drop down
select_elements = driver.find_elements(By.XPATH,"//i[contains(@class,'oxd-select-text--arrow')]")

# click on the first drop down
select_elements[0].click()

# Locate the Admin option from the drop down
admin_option = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@role='listbox']//span[text()='Admin']")))
admin_option.click()
© www.soinside.com 2019 - 2024. All rights reserved.