0

I need a JSON file with network logs. The network logs should be collected by clicking each nav link in the navbar. Here's the website, https://www.lambdatest.com/

The nav links have their own sub nav links. I have written a python script for this using Selenium. But I am getting a StaleElementReferenceException.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
import json


def get_logs():
    capabilities = webdriver.DesiredCapabilities.CHROME.copy()
    capabilities['goog:loggingPrefs'] = {'performance': 'ALL'}
    chrome_options = Options()
    driver = webdriver.Chrome(desired_capabilities=capabilities, options=chrome_options)
    driver.get("https://www.lambdatest.com/")

    links = driver.find_elements(By.CLASS_NAME, "nav-link")
    for link in links:
        if link.tag_name == 'button':
            link.click()
            time.sleep(1)
            parent = link.find_element(By.XPATH, "..")
            unordered_lists = parent.find_elements(By.TAG_NAME, 'ul')
            for i in range(0, len(unordered_lists)):
                a_tags = unordered_lists[i].find_elements(By.TAG_NAME, 'a')
                for j in range(0, len(a_tags)):
                    a_tags[j].click()
    logs = driver.get_log('performance')
    with open('/data/network_logs.json', 'a') as f:
        for log in logs:
            f.write(json.dumps(log) + '\n')
    driver.quit()


if __name__ == '__main__':
    get_logs()

Basically, I need to iterate over the navlinks, navigate to the page and collect the network logs.

  • check here if you can find something helpful https://stackoverflow.com/questions/27003423/staleelementreferenceexception-on-python-selenium – Ajeet Verma Mar 19 '23 at 07:08

1 Answers1

0
The stale element reference Exception occurs when the page is loaded that time the elements got shuffled and selenium APIs are unable to find the elements and it through the stale exception.

this solution might work for you.

Webdriverwait waits=new webDriverWait();
waits.until(ExpectedCondition.invisibleoflocator(driver.find(By.xpath("")));
Bhairu
  • 72
  • 4