Home > other >  How can I choose an option in a list through selenium?
How can I choose an option in a list through selenium?

Time:10-10

My code is:

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='/icpplustieb/citar?p=8&locale=es']"))).click()

The URL is: https://icp.administracionelectronica.gob.es/icpplus/index.html

I would like to choose Barcelona as a Region.

CodePudding user response:

Use selenium select() class

select =Select(WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='form']"))))
select.select_by_visible_text("Barcelona")

You need to import the following libarry.

from selenium.webdriver.support.ui import Select

CodePudding user response:

As i can see select in your dropdown, so you can see like below.

from selenium import webdriver
from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_xpath("//*[@id='form']"))
select.select_by_visible_text('Barcelona')

or

#select by value

select.select_by_value('Barcelona')

CodePudding user response:

You can use Select():

import:

from selenium.webdriver.support.ui import Select

province =  Select(driver.find_element(By.CSS_SELECTOR, "#form"))

# you can use any of the below methods to choose the target option: 
province.select_by_index(9)
# or
province.select_by_value("/icpplustieb/citar?p=8&locale=es")
# or
province.select_by_visible_text("Barcelona")
  • Related