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.
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
Asked: 2023-03-31 11:00:00 +0000
Seen: 10 times
Last updated: Jun 01 '21
How can one open the "What's New" window in IntelliJ?
What is the process to sort the first 50 rows?
If I click on an item, it doesn't seem to be reacting.
How can key and value be saved in admin setting using Opencart 3?
How can a new tab be opened in Windows Terminal with the same environment as the current one?
When I click on "Install Requirements" in PyCharm, why is nothing occurring?
How can the recorded metric be obtained from Micrometer/Prometheus?
How can padding be added to an HTML email signature in Outlook?
How to give an <svg> element a double border using CSS when there is no style attribute present?