Ask Your Question
1

Using selenium, how can I click on every element within the navigation bar of https://www.lambdatest.com/ and save the resulting network logs?

asked 2023-03-31 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-01 12:00:00 +0000

devzero gravatar image

To click on every element within the navigation bar and save the resulting network logs using Selenium, you can follow the below steps:

  1. Import the required libraries:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Enable network logs
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities.CHROME.copy()
cap['goog:loggingPrefs'] = {'performance': 'ALL'}
  1. Launch the Chrome browser and navigate to the website:
driver = webdriver.Chrome()
driver.get("https://www.lambdatest.com/")
  1. Wait for the page to load and get the navigation bar element:
time.sleep(5)    # Wait for the page to load
nav_bar = driver.find_element_by_css_selector(".navbar-nav")
  1. Loop through all the <a> elements within the navigation bar and click on them one by one:
for link in nav_bar.find_elements_by_tag_name("a"):
    link.click()
    time.sleep(5)    # Wait for the page to load
  1. Get the network logs and save them to a file:
# Get the network logs
logs = driver.get_log("performance")

# Save the logs to a file
with open("network_logs.txt", "w") as f:
    for log in logs:
        f.write(str(log) + "\n")

# Close the browser
driver.quit()

The complete code would look like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Enable network logs
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities.CHROME.copy()
cap['goog:loggingPrefs'] = {'performance': 'ALL'}

# Launch the Chrome browser
driver = webdriver.Chrome(desired_capabilities=cap)

# Navigate to the website
driver.get("https://www.lambdatest.com/")

# Wait for the page to load
time.sleep(5)

# Get the navigation bar element
nav_bar = driver.find_element_by_css_selector(".navbar-nav")

# Loop through all the <a> elements within the navigation bar and click on them one by one
for link in nav_bar.find_elements_by_tag_name("a"):
    link.click()
    time.sleep(5)    # Wait for the page to load

# Get the network logs
logs = driver.get_log("performance")

# Save the logs to a file
with open("network_logs.txt", "w") as f:
    for log in logs:
        f.write(str(log) + "\n")

# Close the browser
driver.quit()

This code will open the website, click on every element within the navigation bar, save the network logs to a file named "network_logs.txt", and then close the browser.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-31 11:00:00 +0000

Seen: 10 times

Last updated: Jun 01 '21