System specifications

System version: macOS10.15.7 PyCharm: 2017.03Copy the code

Python interpreter configuration tutorial

If you haven’t already installed the environment, check out our first article, MacOS Web Automation Tutorial (Part 1) – Python3 + Selenium3 + Chrome Environment installation # for the better

Add the Python interpreter

  1. Use the which Python command to obtain the python installation path on your system
sugooddeiMac:bin sugood$ which python
/usr/bin/python
Copy the code
  1. Open Configuration – Preferences

3. To add the Python environment, click the “Set” button in the red circle4. Select the environment in the system and find the Python path. Select the PYTHon3 file in the path5. After the selection is successful, the following page is displayed. Click Apply to Apply

How do I execute a test script

The test script code is as follows

from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('http://www.baidu.com')

time.sleep(3)
print(browser.page_source)
browser.quit()
Copy the code

We ran this test script in two ways

  • First, create a project and configure the interpreter. Then create a script file. Copy the code to the execution file.
  • Second: copy the above code directly into an existing file. Then open it directly, and finally configure the interpreter

Let’s go through each of them

First: Create a project

  1. New project

2. Select the interpreter we configured above and click Create

  1. Create a Python file

5. There are two caveat to this approach. I don’t know if it’s my environment, but there’s a solution

  • After the first creation, closing the project and opening it again will prompt us to configure the interpreter. Don’t worry. Let’s just go in and configure it again
  • The first new Python file has no Python properties bound. You have to create another one

Second: open an existing script file

  1. Create a test.py file. Copy the above test code in and save it.
  2. Using PyCharm to open the test.py file, we see a warning and click in

2. Select the Python interpreter we configured earlier3. Click Apply

Execute the test script

1. Execute the script

2, the final successful interface

Complete the above steps and you will be Ok.

The advanced

Create multiple interpreters

With multiple interpreters, different versions of Python can be opened independently. Different projects choose different environments. The same project can also switch between different interpreters. It’s very convenient to use

  1. Turn on preferences

2. Add an interpreter3. Create a new interpreter4. The following page is displayed5. Customize the required software. The PIP install XXX directive is called. Therefore, the system environment should be configured.

Selenium is not installed in this interpreter. Let’s install Selenium to demonstrate this, as well as other software.

Switch interpreter

Just choose the interpreter you want

conclusion

In fact, there are many tutorials online. But I’m going to clean it up a little bit so I can think clearly when I need to see it myself

series

Python3 + Selenium3 + Chrome environment installation # Good for good

Configure the Python tutorial with PyCharm and execute a simple script # do the right thing

#Python3 + Selenium3+ HTMLTestRunner# Python3 + Selenium3+ htmltestrunner