1 | initial version |
To click on every element within the navigation bar and save the resulting network logs using Selenium, you can follow the below steps:
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'}
driver = webdriver.Chrome()
driver.get("https://www.lambdatest.com/")
time.sleep(5) # Wait for the page to load
nav_bar = driver.find_element_by_css_selector(".navbar-nav")
<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()
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.