python selenium+phantomJS automated test environment

0x00 Configure phantomJS

1. Under the windows platform

 

This method is to pop the browser for automated testing.

1. Download Google Driver

https://chromedriver.storage.googleapis.com/index.html

2. Put the decompressed chromedriver.exe in the installation directory of the chrome browser.

3. Call the browser driver in the code to perform automated operations.

chromedriver = ' chromedriver absolute path ' 
driver = webdriver.Chrome(chromedriver)
driver,get(url)

 

 

 

2. Under the linux command line without interface

 

Using phantomJS with selenium, we can create a browser without interface, which can achieve our purpose.

 

Configuration method:

 

1. Install phantomJS

Installation package download address: http://phantomjs.org/download.html , including Windows, Mac OS, and Linux versions, you can download and unzip the corresponding version by yourself

sudo  wget https: // bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

 

You may encounter this error when unpacking a tar.bz2 file with tar

 

debian:/usr/src# tar jxf linux-2.6.26.tar.bz2

tar: bzip2: Cannot exec: No such file or directory

tar: Error is not recoverable: exiting now

tar: Child returned status 2

tar: Error exit delayed from previous errors

 

Solution:

This problem is when the bzip2 tool is missing, install the tool.

yum install bzip2

 

0x02 Install selenium

 

pip install selenium

to here. The environment is configured. Then you can code.

 

0x03 Sample Code

 

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path= ' /bin/phantomjs/bin/phantomjs ' )#The executable_path here fills in the path of your phantomJS

driver.get('http://www.xxxx.com')
print driver.title
driver.quit()

 

Possible error messages:

 

selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH

 

This kind of error is that your PhantomJS save path is wrong, check whether the filled path is the save path of the phantomjs program.

 

Note that the windows system is used here, you need to add r in front of the path, refer to the code statement given.

browser = webdriver.PhantomJS(executable_path=r'C:\Users\lyh\Anaconda2\phantomjs-2.1.1-windows\bin\phantomjs.exe')

 

Reference article

https://blog.csdn.net/lambert310/article/details/48734389

https://blog.csdn.net/JohinieLi/article/details/76622776

Guess you like

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