Home > Mobile >  How to re-load page while looping over elements?
How to re-load page while looping over elements?

Time:01-07

This is my code, should be easily recreateable:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options


def main():
    # Setup chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # Ensure GUI is off
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--window-size=1920x3500")

    # Set path to chromedriver as per your configuration
    webdriver_service = Service("/home/sumant/chromedriver/stable/chromedriver")

    # Choose Chrome Browser
    driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
    driver.maximize_window()

    # Get page
    url =  "https://www.ibrance.com/"
    driver.get(url)
    time.sleep(2)
    ele = driver.find_elements_by_tag_name('a')
    for i, e in enumerate(ele):
        try:
            print(e.get_attribute('outerHTML'))
            e.click()
            time.sleep(2)
            driver.save_screenshot(f"/mnt/d/Work/ss{i}.png")
            driver.get(url)
            # driver.refresh()
        except:
            print("element not interactable")
    
    driver.close()
    driver.quit()

if __name__ == '__main__':
    main()

The idea is I click on a link take a screenshot, load home page again, click on next link and so on. After the first link, it is not able to find any other element on the reloaded page.

CodePudding user response:

This is correct, since after the refresh it is unable to find you required elements. To do so, elements need to be reloaded after each refresh. Do this:

ele = driver.find_elements_by_tag_name('a')
for i, e in enumerate(ele):
    try:
        print(e.get_attribute('outerHTML'))
        e.click()
        time.sleep(2)
        driver.save_screenshot(f"/mnt/d/Work/ss{i}.png")
        driver.get(url)
        driver.refresh()
        # reload elements
        ele = driver.find_elements_by_tag_name('a')

CodePudding user response:

So this worked (Thanks YuMa, for the inspiration)

def main():
# ...


# Get page
    url =  "https://www.ibrance.com/"
    driver.get(url)
    time.sleep(2)
    total_element = driver.find_elements_by_tag_name('a')
    total_clicks = len(total_element)


    def get_images(ele, i):
        try:
            ele[i].click()
            time.sleep(2)
            # driver.save_screenshot(f"/mnt/d/Work/ss{i}.png")
            print(driver.title)
            driver.get(url)
            time.sleep(2)
        except:
            print("")
    

    for i in range(0, total_clicks 1):
        ele = driver.find_elements_by_tag_name('a')
        get_images(ele, i)
  •  Tags:  
  • Related