使用Selenium选择下拉框选项

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

selecting Dropdown option using selenium

问题

I'm trying to fill this form and can't really select the dropdown options, link
https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser

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

I also can't use select because there is no select tag in the HTML code

我还不能使用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")

this shows an error that select cant be used on a div tag

这显示了一个错误,不能在div标签上使用select。

英文:

I'm trying to fill this form and can't really select the dropdown options, link
https://opensource-demo.orangehrmlive.com/web/index.php/admin/saveSystemUser

I also can't use select because there is no select tag in the HTML code

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")

this shows an error that select cant be used on a div tag

答案1

得分: 0

下拉元素不是用Select标签构建的,所以无法使用Select库与下拉交互。

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

以下是能够单击“管理”选项的代码。

# 用于显式等待的导入
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

等待 = WebDriverWait(driver,30)

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

# 导航到添加用户页面。
time.sleep(20)

# 查找所有下拉菜单的元素
select_elements = driver.find_elements(By.XPATH,"//i[contains(@class,'oxd-select-text--arrow')]")

# 单击第一个下拉菜单
select_elements[0].click()

# 从下拉菜单中找到“管理”选项
admin_option = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@role='listbox']//span[text()='Admin']")))
admin_option.click()
英文:

The drop down element is not build with Select tag, so Select library cannot be used to interact with the drop down.

Also the options are disappearing while trying to inspect them. Refer How can I inspect disappearing element in a browser?

Below is the code where I was able click on Admin option.

# 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()

huangapple
  • 本文由 发表于 2023年5月7日 20:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193973.html
匿名

发表评论

匿名网友

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

确定