Skip to content
Advertisement

Select value from dropdown and click on search

I want they select value for the dropdown from the Ville option and then click on search option for each value but the problem is that they do not select value from ville these is pag link https://www.barreaunantes.fr/annuaire/

enter image description here

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select

PATH="C:Program Files (x86)chromedriver.exe"
url='https://www.barreaunantes.fr/annuaire/'
driver =webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 20)
driver.get(url)

select=driver.find_element(By.CSS_SELECTOR,"select[id='ville-select']" )
options = select.find_elements(By.TAG_NAME,"option")

for option in options[1:]: 
        t=option.get_attribute("value")
        
d=driver.find_element(By.CSS_SELECTOR,"input[type='submit']")
d=d.click()

Advertisement

Answer

Your element locator strategy selection isn’t in proper way. The following code is working as expectation. You can increase or decrease the range of value selection yours own way.

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

options = Options()
options.add_argument("--no-sandbox")
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service,options=options)
url = 'https://www.barreaunantes.fr/annuaire/'
driver.get(url)

for x in range(4):
    select = Select(WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#ville-select'))))
    select.select_by_index(x)
    time.sleep(2)
    
    click_on_search_button = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '(//*[@value="Rechercher"])[1]')))

    driver.execute_script("arguments[0].click();", click_on_search_button)     
Advertisement