Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

python - Firefox started by Selenium ignores the display created by pyvirtualdisplay

I start a display with pyvirtualdisplay before creating my WebDriver instance. If I use Chrome, it works without any problem: Chrome shows up in the Xephyr instance that pyvirtualdisplay creates. However, when I use Firefox, the Xephyr instance appears but Firefox shows up outside of it.

Here is code that fully reproduces the problem:

from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxBinary
import pyvirtualdisplay

binary = FirefoxBinary()

with pyvirtualdisplay.Display(visible=True):
    if True:  # Set to False to use Chrome...
        driver = webdriver.Firefox(None, binary)
    else:
        driver = webdriver.Chrome()

    driver.get("http://www.google.com")
    driver.quit()

The code above is a minimal version of code that is more complex (which explains the seemingly useless FirefoxBinary()).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution

Move your creation of the FirefoxBinary object inside the context managed by pyvirtualdisplay.Display:

from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxBinary
import pyvirtualdisplay

with pyvirtualdisplay.Display(visible=True):
    if True:  # Set to False to use Chrome...
        binary = FirefoxBinary()
        driver = webdriver.Firefox(None, binary)
    else:
        driver = webdriver.Chrome()

    driver.get("http://www.google.com")
    driver.quit()

Explanation

The problem is what happens behind the scenes. The environment variable named DISPLAY is what determines where Firefox and Chrome will connect to. It is not set in the way you'd expect it to be set.

Here is what happens with your code:

  1. You create an instance of FirefoxBinary. If you read the source code, you'll see that when an object of this class is created it makes a copy of os.environ (the environment).

  2. You create a display with pyvirtualdisplay.Display and use it as a context manager. As you enter the context, the display alters os.environ so that as long as the context is in effect, the DISPLAY environment variable is set so that X clients will connect to the new display instead of what DISPLAY was before the context came into effect.

  3. You create your driver. When you use Chrome, everything is fine because Chrome will get its DISPLAY variable from the modified environment. However, when you use Firefox, it will use the DISPLAY environment from the environment that was copied in the first step. This environment contains a value of DISPLAY which is prior to the alteration described in the previous step, so it does not connect to the new display you created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...