Home > Mobile >  Python Selenium Select dropdown table not selecting?
Python Selenium Select dropdown table not selecting?

Time:11-05

I am trying to use the python selenium module to get past a alcohol website 'are you 18 webpage' check. From the website I am trying to select a region from a dropdown table and the python Select class I pass it through registers but no change is made in the chrome webdriver. Here is what I have so far, any advice would be greatly appreciated. Website is: https://www.shop.liquorland.co.nz/splash.aspx?ReturnUrl=/

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



def main(website):
    """This function will be set to work with standard superliquor website format to get store details for the database"""
    #load webpage
    driver = webdriver.Chrome(executable_path='chromedriver_win32/chromedriver.exe')
    driver.get(website)
    #accept "I am ages 18 or over" button
    acceptage = driver.find_element_by_xpath("/html/body/div[1]/header/div[2]/input[1]").click() #working

    #select region, something not working below
    regionselect = Select(driver.find_element_by_xpath("/html/body/div[1]/main/form/fieldset/div[1]/select"))#.get_attribute('outerHTML')
    regionselect.select_by_index(3) #to test, try selecting Northland

    #From here, I expect that the webpage region select would be set to Northlands
    #NOTE: Yes I have tried regionselect.select_by_value("3") and select_by_visible_text("Northland")


    
if __name__ == "__main__":
    main('https://www.shop.liquorland.co.nz/splash.aspx?ReturnUrl=/')

CodePudding user response:

Unfortunately there is some custom select/dropdown implementation (probably this one), so you need to manually invoke dropdown, and click desired option:

driver.find_element_by_xpath("/html/body/div[1]/main/form/fieldset/div[1]/span").click()
driver.find_element_by_xpath('/html/body//div[contains(@class,"jcf-select-drop-content")]//span[@data-index="2"]').click()

Note: How I got to know a way to invoke dropdown? When you click on a dropdown you can see that <span> below <select> is changing it's classes so it should be this element.

CodePudding user response:

When you inspect the options under the drop-down, the elements are in a ul - li tag not in Select tag.

Can select the required option like below:

# Imports required:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://www.shop.liquorland.co.nz/splash.aspx?ReturnUrl=/")

# click on age button
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"(//input[@type='button'])[1]"))).click()

# Click on the drop-down
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Region']/parent::span"))).click()

# Select option using indexing
# region = driver.find_element_by_tag_name("ul")
# options = region.find_elements_by_tag_name("li")
# options[4].click()

# Select option using specific text
region = driver.find_element_by_tag_name("ul")
option = region.find_element_by_xpath("./li/span[text()='Auckland']")
option.click()
  • Related