Categories
Python Answers

How to run Python Selenium in Xvfb?

Spread the love

Sometimes, we want to run Python Selenium in Xvfb.

In this article, we’ll look at how to run Python Selenium in Xvfb.

How to run Python Selenium in Xvfb?

To run Python Selenium in Xvfb, we can use pyvirtualdisplay.

To install it, we run

pip install PyVirtualDisplay

Then we use it by writing

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.example.com')
print(browser.title)
browser.quit()

display.stop()

to create the Display and call start on it to start it.

Then we start the Firefox driver with

browser = webdriver.Firefox()

Then we open the webpage we want with get.

And then we get the title of the browser window with browser.window.

Finally, we call browser.quit to exit the browser and display.stop to stop the display.

Conclusion

To run Python Selenium in Xvfb, we can use pyvirtualdisplay.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *