Skip to content Skip to sidebar Skip to footer

Selenium/python I Cant Select A Item In Dropdown

I am writing an automation for work and am stuck with a dropdown. The particular select box in question is as follows: enter image description here If there is no select, how can I

Solution 1:

A demo in Java language, just translate to Python by yourself (if you can't, let me know):

WebElementprovince= driver.findElement(By.xpath("//*[@id='province']"));

// scroll the page to the element `Provincia`JavascriptExecutorjse= (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", province);

// province.click(); doesn't work, I have not figured out whynewActions(driver).moveToElement(province).click().perform();

// Notice this is the answer to your question
driver.findElement(By.xpath("//ul//li//span[contains(text(),'Granada')]")).click();

Solution 2:

As is is not a select type item, you can first click on arrow in drop down to make available options visible. Then you can use Java script to select desired option from drop down. Reason we use javascript because it might be possible even after clicking on arrow few options may not be in screen freame.

driver = webdriver.Chrome('..\drivers\chromedriver')
driver.maximize_window()
driver.get("https://www.milanuncios.com/publicar-anuncios-gratis/formulario?c=393")# Wait for upto 20 sec for page to load completely
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[@class='ma-NavigationHeader-logoLink']")))

proviceDropDown = driver.find_element_by_xpath("//input[@id='province']//following-sibling::span")
#Scroll to dropdoen you want to select
driver.execute_script("arguments[0].scrollIntoView();", proviceDropDown)
proviceDropDown.click()


#Assume you are getting province name as parameter, I am doing a static assignment for demonstration 
proviceValue = "Granada"
optionToSelectXPath = "//ul[@class='sui-MoleculeDropdownList sui-MoleculeDropdownList--large']//span[text()='"+ proviceValue +"']")"
driver.execute_script("arguments[0].click();", driver.find_element_by_xpath(optionToSelectXPath ))

Post a Comment for "Selenium/python I Cant Select A Item In Dropdown"