Home > OS >  Python Selenium how to loop through span dropdown with no options
Python Selenium how to loop through span dropdown with no options

Time:03-16

I am working with this website: https://www.offerte.smartpaws.de/

I came with this dropdown for breeds (Rasse) where unlike other droptdowns I do not find the option description list that allows me to loop through each element.

The values change based on the previous element (yes, no right above)

I am looking to loop through each option in this droptown but can't figure out how.

so far I have this code to try to loop through it:

#y is based on previous element being yes or no, since dropdown changes based on it
            if y == 'yes':
          #size is a list of a few examples inside the dropdown, Ideally I don't want to have a list hardcoded but to go through all the options available
            for size in sizes:
#below I click on the dropdown to display the elements
                WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-id_form-0-breed-container"]'))).click()
 #below I try to click on the option containing the text in the list
                WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
                                                                        '//select[@id="select2-id_form-0-breed-container"]//option[text()='  
                                                                        size  ']'))).click()
 #below is the same step but for option "no" in the previous element, and same logic as the previous step
        else:
            for breed in breeds_dog:
                WebDriverWait(driver, 10).until(
                    EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-id_form-0-breed-container"]'))).click()
                WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
                                                                            '//select[@id="select2-id_form-0-breed-container"]//option[text()='  
                                                                            breed   ']'))).click()

The goal is to be able to loop through all the breed option (for dogs for now) ideally without having to store the list in the code, but to loop through the options. As we can see in the website this element does not store a list of options.

Thanks

CodePudding user response:

This code would work:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-id_form-0-breed-container"]'))).click()
breeds = driver.find_elements(By.XPATH, "//span[@class='select2-results']//li")
print(len(breeds))
for each_breed in breeds:
    print(each_breed.text)

Output (only part of output is pasted here as there are 534 elements in the dropdown):

534
Rasse
Abessinier
Affenpinscher
....
Yorkshire Terrier
Zwergdackel
Zwergpinscher/Min Pin
Zwergpudel
Zwergschnauzer
Zwergspaniel

Process finished with exit code 0

Note: I haven't performed any click action on each of the dropdown item. I just brought out each item's text and printed to console.

The elements are located somewhere else. I found through inspecting on the dropdown item:

DOM Snapshot

--- UPDATE --- :

I tried this rather clumsy workaround to click on the each element of dropdown (the above method somehow hits me with stale element error):

The below code finds each item in dropdown and clicks on it.

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-id_form-0-breed-container"]'))).click()
breedpath = "//span[@class='select2-results']//li"
breeds = driver.find_elements(By.XPATH, breedpath)
print(len(breeds))
for i in range(len(breeds)):
    each_breed = driver.find_element(By.XPATH, '('   breedpath   '['   str(i 1)   ']'   ')')
    print(each_breed.text)
    each_breed.click()
    # ... your code here ...
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@role='combobox']"))).click()
  • Related