Selenium+python Environment construction under MAC

Source: Internet
Author: User
Tags python script

Selenium2+python's environment is mainly built on Python and selenium.

1.python

The Mac comes with Python and can view the version. Of course, you can choose to install other versions of Python.

2.selenium

Install with sudo easy_install selenium in the Mac's own terminal

Through the above two steps to complete the most basic installation of the environment. Firefox is generally chosen as the default browser for automated testing, and of course other browsers are available.

In Firefox, for example, we can execute the following Python script to test whether selenium is working properly.

 1  from  selenium import   Webdriver  2  import   time  3  Dr = Webdriver. Firefox ()  4  time.sleep (5 5  print   " Span style= "COLOR: #800000" >browser'll be closed   " 6  dr.quit ()  7  print  "  browser is close   " 

The following error is encountered when executing the above script:

Traceback (most recent): File"/users/xxx/documents/selenium_py/inittest/test.py", Line 3,inch<module>Dr=Webdriver. Firefox () File"/library/python/2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 144,inch __init__Self.service.start () File"/library/python/2.7/site-packages/selenium/webdriver/common/service.py", line 81,inchstart Os.path.basename (self.path), self.start_error_message) Selenium.common.exceptions.WebDriverException: Message:'Geckodriver'Executable needs to beinchPATH. [Finishedinch0.1s with exit code 1]

The reason is that the version of Firefox used is 56.0, for the newer version of the Firefox browser, you need to download the corresponding driver geckodriver. Put the downloaded driver in the directory you want.

Modify the original script as shown below and use the specific drive location as the entry parameter.

 fromSeleniumImportWebdriverImportTimedr= Webdriver. Firefox (Executable_path ='/users/xxx/documents/selenium_py/geckodriver/geckodriver')#dr = Webdriver. Chrome (Executable_path = '/users/xxx/documents/selenium_py/chromdriver/chromedriver ')Print'Browser 'll be closed'dr.quit ()Print'Browser is close'

For other browsers only need to download the corresponding driver on the official website http://www.seleniumhq.org/download/.

After adding the driver position in the script, there was an error.

Traceback (most recent): File"/users/xxx/documents/selenium_py/inittest/test.py", Line 3,inch<module>Dr= Webdriver. Firefox (Executable_path ='/users/xxx/documents/selenium_py/geckodriver/geckodriver') File"/library/python/2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 144,inch __init__Self.service.start () File"/library/python/2.7/site-packages/selenium/webdriver/common/service.py", Line 102,inchStartRaiseWebdriverexception ("Can not connect to the Service%s"%self.path) Selenium.common.exceptions.WebDriverException:Message:Can notConnect to the service/users/xxx/documents/selenium_py/geckodriver/geckodriver[finishedinch38.3s with exit code 1]

Solve this problem in the/etc/hosts file will be 127.0.0.1 localhosts Plus.

Then you can run the script properly.

Selenium+python Environment construction under MAC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.