Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium with Python, how do I get the page output after running a script?

I'm not sure how to find this information, I have found a few tutorials so far about using Python with selenium but none have so much as touched on this.. I am able to run some basic test scripts through python that automate selenium but it just shows the browser window for a few seconds and then closes it.. I need to get the browser output into a string / variable (ideally) or at least save it to a file so that python can do other things on it (parse it, etc).. I would appreciate if anyone can point me towards resources on how to do this. Thanks

like image 928
Rick Avatar asked Mar 16 '26 15:03

Rick


2 Answers

using Selenium Webdriver and Python, you would simply access the .page_source property to get the source of the current page.

for example, using Firefox() driver:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get('http://www.example.com/')

print(driver.page_source)

driver.quit()
like image 116
Corey Goldberg Avatar answered Mar 18 '26 03:03

Corey Goldberg


There's a Selenium.getHtmlSource() method in Java, most likely it is also available in Python. It returns the source of the current page as string, so you can do whatever you want with it

like image 43
Sergii Pozharov Avatar answered Mar 18 '26 03:03

Sergii Pozharov