I want to capture all packets related to a page loading when using
selenium
. I'm collecting the timestamps myself and comparing them to the packets data from wireshark.
When I do this it shows me the start time but not the end time.
Here is my code:
class Test():
def packet_trace(self,url):
domain = url.split('.')[0]
path = '/path/to/save'
packet_details_path = os.path.join(path, domain + '.pcap')
self.capture = pyshark.LiveCapture(interface='enp0s25', output_file=packet_details_path)
for packet in self.capture.sniff_continuously(packet_count=self.network_trace):
print(packet)
def connection_firefox(self):
url_list = ['facebook.com','apple.com','google.com']
for url in self.url_list:
try:
full_url = 'https://' + url
domain = url.split('.')[0]
driver = webdriver.Firefox()
try:
driver.set_page_load_timeout(self.timeout)
self.packet_trace(url)
path_trace_details = '/path/to/save'
start_time = time.time()
driver.get(full_url)
end_time = time.time()
total_time = end_time - start_time
packet_info_path = os.path.join(path_trace_details, domain + '_info.txt')
with codecs.open(packet_info_path, 'w+' , 'utf-8') as write_info:
write_info.write(f'url:: {full_url} \nstart_time:: {start_time}\n entry_node:: {circuit_entry_node} \nend_time:: {end_time} \ntotal_time:: {total_time}')
except TimeoutException as e:
path = '/path/to/save'
write_error = os.path.join(path, 'timeout_in_' + domain + '.txt')
write_mode = codecs.open(write_error, 'w+' , 'utf-8')
write_mode.write(f'url:: {full_url} \n time out error:: {e} \n -----------------------------------------------')
driver.close()
continue
except Error as e:
path = '/path/to/save'
write_error = os.path.join(path, domain + '.txt')
write_mode = codecs.open(write_error, 'w+' , 'utf-8')
write_mode.write.write(f'url:: {full_url} \n error:: {e} \n -----------------------------------------------')
driver.close()
continue
test = Test()
test.connection_firefox()
I don't know why it's working like this. once it reach to self.packet_trace(url) the code finish running the function. and I want to run the capturing packet section until a page completely load.