Python+selenium environment installation and configuration and examples

1 install python

Download Python 3.6

Note: If you have a python3.6 installation package in your hand, skip the step of downloading Python 3.6.

Download python3.6.x, the official website download address is: https://www.python.org/downloads/ , select the appropriate version, click download, enter the download page

 

 

Select the executable installer format file on the download page and click to download:

 

Install Python

Double-click the downloaded python-3.6.4.exe and run the downloaded EXE installation package. Be sure to check Add Python 3.6 to Path, otherwise it is particularly inconvenient to use. Then click Install Now to install.

 

 

 

Run python

After the installation is successful, open the CMD command prompt window and enter python. If something similar to the following appears, the python installation is successful; if it does not appear, it is most likely that the Add Python 3.6 to Path was not checked during the installation process. Please refer to the online python tutorial on adding environment variables.

 

2 Install selenium

Open the CMD window and enter:

pip3  install selenium

Selenium will be installed automatically. If no error is reported and the installation is successful, selenium will be installed successfully.

We can verify whether the installation is successful and can be used normally, the verification method:

Open the IELD that comes with python, enter from selenium importwebdriver, and hit enter. If no error is reported, selenium is successfully installed and can be used normally.

 

3 Install pycharm

After the basic environment is installed, we need an easy-to-use IDE for script writing. Personally recommend pycharm, you can also choose according to your own preferences.

  Go to https://www.jetbrains.com/pycharm/download/#section=windows to download pycharm. Professional is a professional version, powerful, but it needs to be charged

Community is a community version, generally the function can be used, and it is free

You can choose according to personal needs.

 

 

For specific installation steps and usage methods, please refer to online materials.

 

4 Install firefox (Firefox) browser and related plug-ins

Why do we need to install the firefox browser? The reason is that the firefox browser has two plug-ins that we need to use:

Selenium IDE: script recording can be achieved

Firebug: Help us locate elements

4.1 install firefox

First, we download firefox, because Selenium IDE cannot support the latest version of firefox browser, so we choose version 54 of firefox, the download link is as follows:

1.  http://ftp.mozilla.org/pub/firefox/releases/54.0/win64/zh-CN/

2. Download links for multiple versions of Firefox (very important):    http://ftp.mozilla.org/pub/firefox/releases/              

The installation process is omitted

Then open the firefox browser, click , and then click to enter the settings page

Click Advanced>>Update>>Do not check for updates, in order to prevent firfox from not supporting selenium IDE after automatic update.

 

 

4.2 Install Selenium IDE plugin

Open the link with firefox browser:

https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

The following screen appears, click + Add to Firefox to add the plug-in. After installation, restart the browser to take effect.

 

 

After restarting the browser, open Selenium IDE, the following interface appears, then the plug-in is successfully added:

 

 

4.3 Add Firebug plugin

Open the link with firefox browser:

https://addons.mozilla.org/zh-CN/firefox/addon/firebug/

The following screen appears, click + Add to Firefox to add the plug-in. After installation, restart the browser to take effect.

After restarting the browser, hit F12 on the keyboard and get the following interface, then the plug-in is added successfully:

 

5 Install Firefox driver

The above environment is installed, we need to think about such a problem. How do Python and selenium operate the browser?

 

We need to install the driver of the browser. Different browsers have different drivers. For firefox, we need to open the following link:

 

https://github.com/mozilla/geckodriver/releases

 

Choose the appropriate version, because I am a 64-bit windows system, so I choose to download geckodriver-v0.19.1-win64.zip

 
 
 

After the download is complete, unzip geckodriver-v0.19.1-win64.zip to the root record of python. Here, my python installation path is E:\Python\Python36-32\Scripts, so I unzip it to E:\Python\Python36-32\Scripts.

 

6 Test the entire environment and code

 Run the following code, if you can successfully open the Tinglv system page, the environment is set up:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Pyauto0808(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(5)
        self.base_url = "https://www.imguider.com/manager/index.html"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_pyauto0808(self):
        driver = self.driver
        driver.get(self.base_url + "/manager/index.html")
        driver.find_element_by_name("username").clear()
        driver.find_element_by_name("username").send_keys("erha")
        driver.find_element_by_name("password").clear()
        driver.find_element_by_name("password").send_keys("erha123")
        driver.find_element_by_id("login").click()
        driver.find_element_by_css_selector("span.title").click()
        driver.find_element_by_css_selector("li.orders > a > span.title").click()
        driver.find_element_by_link_text("2").click()
        driver.find_element_by_link_text("3").click()
        driver.find_element_by_xpath("//div[@id='left']/div/ul/li[8]/a/span").click()
        driver.find_element_by_link_text(u"优惠券管理").click()
        driver.find_element_by_id("add-coupon2").click()
        driver.find_element_by_css_selector("input.layui-layer-input").clear()
        driver.find_element_by_css_selector("input.layui-layer-input").send_keys("1")
        driver.find_element_by_link_text(u"确定").click()
        driver.find_element_by_css_selector("input.layui-layer-input").clear()
        driver.find_element_by_css_selector("input.layui-layer-input").send_keys("0808")
        driver.find_element_by_link_text(u"何晨").click()
        driver.find_element_by_link_text(u"退出登录").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

 

 

 

Guess you like

Origin blog.csdn.net/yang520java/article/details/81513284