Selenium python: iterating through dropdown element - number of items varies on page

124
April 15, 2022, at 05:10 AM

I have a repository of pages accessible from a Home page. I have a selenium script that currently loops through each of these (a set number of pages) as I would like, telling me if an iframe on the respective page loads certain elements or not.

    home = "url.com/home"
    driver.get(home)
    
    for page in range(1,30):
        page = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, f"/{page}/button")))
        page.click()
        homeBtn = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//*")))
    
        try:
            iframe_element = WebDriverWait(driver, 20).until(
                EC.visibility_of_element_located((By.XPATH, "//*")))
            driver.switch_to.frame(iframe_element)
            viz = WebDriverWait(driver, 20).until(
                EC.visibility_of_element_located((By.ID, "page-region")))
            driver.switch_to.default_content()
            print(f"Page container ({page}) visible")
            homeBtn.click()
    
        except:
            print(f"The page ({page}) has failed to load.")
            driver.switch_to.default_content()
            homeBtn.click()

Some of the pages that this iterates through also contain a dropdown consisting of clickable elements that load essentially a new page with a different view/iframe, which I would also like to interact with in the same manner to ensure the iframe on the new view is loading. I have a script that is able to interact with the dropdown and navigate to specific selected views of the dropdown or to even iterate through it in a similar fashion to the original script if the range is known.

view_dropdown = WebDriverWait(driver, 20).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@id="views-dropdown"]')))
view_dropdown.click()
view = WebDriverWait(driver, 20).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@id="views"]/div[1]/a')))
view.click()
view2 = WebDriverWait(driver, 20).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@id="views"]/div[2]/a')))
view2.click()

However, since the dropdowns have a varying number of items depending on the page (and some do not have any dropdown) I do not know how to use this with my original script. My thought is to go to each page and first try to interact with a dropdown, which if successful will iterate through the original script in a nested try/except to check the iframe on each view, with the parent except block also containing a similar nested try/except. However I don't know how to construct this if the dropdown is of a variable length


for page in range 
        try:
            #If dropdown present
                 for i in dropdown # of varied length depending on page
                      click i
                          try: 
                               iframe loads correctly
                          except: 
                               iframe fails to load 
        except:
                          try: 
                               iframe loads correctly
                          except: 
                               iframe fails to load 

 
Rent Charter Buses Company
READ ALSO
How to plot in edges and routes on the same folium in osmnx

How to plot in edges and routes on the same folium in osmnx

I'm trying to plot edges and routes in an interactive map by using: oxplot_graph_folium or ox

116
Capturing stdout result of `flutter test integration_test`

Capturing stdout result of `flutter test integration_test`

For CI, I need to write a program that runs flutter test integration_test and captures the stdout resultIn python the code would be:

94
Replace a pattern in column of a csv file using Python

Replace a pattern in column of a csv file using Python

I am looking for some help with editing CSV in pythonI have a file in below format

125
Making animation on matplotlib of graph with Networkx efficiently

Making animation on matplotlib of graph with Networkx efficiently

I am trying to animate a graph whose edges widths and color change over timeMy code works, but it is extremely slow

118