Home > Back-end >  Select with selenium in python
Select with selenium in python

Time:02-23

I wish to use Select to click on the 3rd option in the 1st drop-down ("Fejlesztési programok") menu at the following site: https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M

I used css-selector to find the menus id, but it is not reacting to the call. Do you have any idea how to solve it? Many thank in advance

The minimalist example code:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Safari(executable_path = '/usr/bin/safaridriver')

driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")

select = Select(driver.find_element_by_id('css-1uccc91-singleValue'))
select.select_by_index(2)

CodePudding user response:

There are several issues here:

  1. You should add a delay to try locating the element only when it's accessible.
  2. The element you are trying to access is not select.
    This should work better:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Safari(executable_path = '/usr/bin/safaridriver')
wait = WebDriverWait(driver, 20)

driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='form-group' and .//label[@for='programok']]//div[contains(@class,'css-1wy0on6')]"))).click()

Now you can select one of the presented options.
But again, this is not a Select element here.

  • Related