A
A
Akshin Yolchuev2020-03-26 13:39:00
Python
Akshin Yolchuev, 2020-03-26 13:39:00

What to do if the page takes a long time to load selenium python?

Using the Selenium library, I have to go to the same site many times and wait for the page to load for 5 seconds. How is it possible to save this page so that it does not load for a long time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-03-26
@bacon

No way, if the page could be saved, then you would probably use requests, not selenium.

S
Sergey Grebennikov, 2020-03-27
@SergeyGrebennikov

You can do something like this:

from selenium import webdriver
driver = webdriver.Chrome('D:\\chromedriver.exe')

# грузим страницу с сайта
driver.get('https://test.com')
 
# сохраняем страницу в файл
with open('D:\\Temp\\SaveHtm.html', 'bw') as out_file: 
    out_file.write(driver.page_source.encode('utf-8', errors = 'strict'))
    out_file.close()
# грузим страницу из файла
driver.get('file:///D:/Temp/SaveHtm.html')

Pay attention to the 'bw' file open mode, the encode(...) method returns a sequence of bytes, which is written to the file. encode is needed so that Russian letters are displayed normally, and not a set of krakozyabr. The utf-8 encoding is given as an example. It is determined from the encoding of the page from the site.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question