Install Python+Selenium+Firefox learning environment under Centos7

Selenium is an automated testing tool. It supports mainstream interface browsers such as Chrome, Safari, Firefox, etc. If you install a Selenium plug-in in these browsers, you can easily implement web interface testing.

So, if you want to use Selenium, you must first have a browser (most people don't say this, it's a little pit!), whether it is Chrome or Firefox, you must have one.

After installing the browser, I installed Selenium with pip3 in a happy mood, imported Webdriver directly in the Pyhton interactive mode, and created the Webdriver object and reported the following error:

1
No such file or directory: 'geckodriver'

What the hell is going on here? It turns out that you also need to install the browser driver files. . . . . . . , the download page is as follows,

1
https: / / github.com / SeleniumHQ / selenium / blob / master / py / docs / source / index.rst

Remember to download the corresponding driver. Chrome cannot use Firefox, and Firefox cannot use Chrome. If the driver version is wrong, an error that cannot be executed will be reported, as follows

1 OSError: [Errno 8] Exec format error

Why not get one with all the drivers together? I don't understand.

Unzip the downloaded driver file to /usr/bin, or /usr/local/bin, which is also in your environment variable. How to modify the PATH directly, I think it is also possible, as long as you can find it.

At this point, I feel like it should be OK, but no, it is no problem to create an object, but when I access the URL, I get an error again. . . . .

1
geckodriver.log Error: GDK_BACKEND does not match available displays

The error message seen from geckodriver.log needs a display. I wanted to use the command line, but it prompts that a matching display is required. After googling, I need to create a virtual display.

Install the following software to create:

yum install Xvfb libXfont xorg-x11-fonts*
pip3 install  pyvirtualdisplay

After jumping out of these pits, it can finally be used normally. The test code is as follows:

copy code
1 from selenium import webdriver 2 from pyvirtualdisplay import Display 3 4 5 display = Display(visible=0, size=(800,600)) 6 display.start() 7 driver = webdriver.Firefox() 8 driver.get("http://www.baidu.com") 9 print driver.page_source 
copy code

In this way, the HTML statement was successfully printed out, and the big hole was successfully jumped out!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324922147&siteId=291194637