英文:
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
答案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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论