无法在Selenium Python中选择下拉菜单的默认值。

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

Can't able to select default value from dropdown in selenium python

问题

我尝试了所有的方法,但无法从下拉菜单中选择默认文本值。

尝试以下方法:

select_responsibility_status = driver.find_element(By.CLASS_NAME, "select-handle")
select_responsibility_status.click()
select_responsibility_status.select_by_visible_text("All responsibilities")

下拉菜单有5个元素,在我的情况下,我只需要选择"All responsibilities"元素。

英文:

I have tried all the way, but not able to select default text value from dropdown

tried below method

select_responsibility_status = driver.find_element(By.CLASS_NAME,"select-handle")
select_responsibility_status.click()
select_responsibility_status.select_by_visible_text("All responsibilities");

The dropdown is having 5 elements, in my case i have to select "All responsibilities" element only

无法在Selenium Python中选择下拉菜单的默认值。

无法在Selenium Python中选择下拉菜单的默认值。

答案1

得分: 1

.select_by_visible_text() 仅适用于已创建为 Select 类的 SELECT HTML 元素,例如:

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.NAME, 'name'))
select.select_by_visible_text("text")

在您的情况下,看起来像是一个下拉菜单,但不是一个 SELECT 元素下拉菜单。您可以通过下拉选项是 LI 标签来判断。在这种情况下,您需要点击下拉元素以显示选项(如您发布的图片中所示),然后再点击所需的元素:

driver.find_element(By.CLASS_NAME, "select-handle").click()
driver.find_element(By.XPATH, "//li[text()='All responsibilities']").click()
英文:

.select_by_visible_text() is only available for a SELECT HTML element that has been created as a Select class, e.g.

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.NAME, 'name'))
select.select_by_visible_text("text")

In your case, it looks like a dropdown but not a SELECT element dropdown. You can tell that because the dropdown options are LI tags. In this case, you'll need to click the dropdown element exposing the options (as shown in the picture you posted) and then click the desired element.

driver.find_element(By.CLASS_NAME,"select-handle").click()
driver.find_element(By.XPATH,"//li[text()='All responsibilities']").click()

huangapple
  • 本文由 发表于 2023年3月15日 19:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744076.html
匿名

发表评论

匿名网友

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

确定