Example of python+selenium implementing automatic clicking after logging into an account

  • 2020-06-19 10:52:48
  • OfStack

In codereview, the company limited the time to look at the code. In fact, many codes are generated automatically by the framework and do not need to spend too much time to look at them. In order to reach the standard, it needs to spend some time (click the fixed area of the webpage with the mouse). It occurred to me that automated testing could be used to accomplish this kind of ineffective manual labor.

First, define the following requirements:

Automatically open web page Log in to your account Click 1 to fix the area every 1 fixed time

There are two solutions that Come to mind, sikuli or python+selenium. The advantages of sikuli are simple and direct logic operation and the use of pictures as a marker. The disadvantages are that it needs fixed Windows and cannot be run in the background. selenium is a bit more complicated and 1 fixed, but it runs fast and the Windows can be blocked.

Here's a quick note of a small example of using Python+selenium.

The versions used are Python3.3 and selenium2,Windows environment (now supports more than 3.0, many forum blogs have not been updated).

The first is the installation of the software, Python will not go into details, remember to set the environment variables.

Install selenium below, if you have already installed pip. Run 1 command directly.


pip install -U selenium

Another 1 kind of way, https: / / pypi python. org/packages/source/s selenium/selenium - 2.52.0. tar. gz download and unpack. Here briefly 1 windows version, in fact Unix is similar, using Wget download 1 under the installation.

Use the command (setup1 generally used for party 3 module installation) :


cd c:\Python3\xxxx
python setup.py install

ImportError: No module named setuptools may appear during installation because of the lack of setuptools module and Python does not install by default.

In http: / / pypi. python. org pypi/setuptools above provides the installation package and each system installation guide, for Windows system, download https: / / bootstrap pypa. io/ez_setup py automation installation script.

Run:


python ez_setup.py

After completion, install selenium.

Here is a simple example of my own to illustrate 1 process.

The first step is to open the browser.

selenium2 combines selenium and webdriver, directly introducing the corresponding driver of each browser, just open, note that the chrome driver may need to be installed separately.


from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.xxx.com')

After opening the web page, you need to log in. F12 opens the browser debugger, selects the element with small arrow, and checks the attributes of login box account and password. Generally, ID is available. selenium can obtain elements and perform various operations through the following methods, as explained in the linked document above:

find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector

id is the most effective and convenient, giving priority. After selecting the element, WebDriver API can be used to call the simulated keyboard input and mouse click operation. The code is as follows:


username="qun" 
passwd="passwd"
browser = webdriver.Firefox()
browser.get('https://www.xxx.com')
browser.implicitly_wait(10)
elem=browser.find_element_by_id("loginFormUserName")
elem.send_keys(username)
elem=browser.find_element_by_id("loginFormPassword")
elem.send_keys(passwd)
elem=browser.find_element_by_id("loginFormSubmit")
elem.click()

1 Generally, the page will jump to a new page after logging in. How to get a new page? There is a window handle concept, done by switching window handles. Attention! Sometimes elements inside an frame also need to be toggled through swtich. An wait function appears (as well as above) because the page takes time to load and the elements are likely to load only after a click, as explained in the next section.


browser.implicitly_wait(10)
browser.switch_to_window(browser.window_handles[-1])

Then select the area that needs to be clicked, xpath is used here, because in the process of automated testing, it is very likely that elements cannot be located by id, name and other methods (many people just don't write, love table sets of table, I do not have the method), xpath has its use. The common lazy method is Firefox install xpath plug-in, right-click directly to get. I'm not going to cover it here, because it's not recommended that you use plugins to fill your code with things like:


XPath ( /html/body/div/div[3]/div[2]/div[4]/p[2] ) 

As a last resort, use the features of elements, such as name for buttons, whenever possible.

Or locate the child element by the parent element.


username =browser.find_element_by_xpath("//input[@name='username']")
clear_button = browser.find_element_by_xpath("//form[@id='loginForm']/input[4]")

The code is as follows: the frequent usage of By on the web needs to be introduced into the package.


from selenium.webdriver.common.by import By

I'm going to use another function here, I don't know what the difference is --.


for i in range(100):
  elem=WebDriverWait(browser, 30).until(
    lambda x:x.find_element_by_xpath("//table[@class='aaa']"/td[1]))
  elem.click()
  time.sleep(20)
  print ("click",i)

Then there is the wait function. In selenium2, there are two types of delay, display wait and implicit wait.

According to wait

An explicit wait is an explicit wait for an element to appear or for an element to be clickable. If you can't wait, wait until 1. Unless you can't find it within the specified time, then jump out of Exception.


cd c:\Python3\xxxx
python setup.py install
0

An implicit wait

Note that implicit waiting tells all dom elements to try again for so long if they are not immediately found while looking for an element.


cd c:\Python3\xxxx
python setup.py install
1

The difference between the two is that one manages the timeout object itself and the other is left to webdriver to do.

Also can use the method of dormancy to do of course. Remember to bring in the time package.


cd c:\Python3\xxxx
python setup.py install
2

This is just a simple demonstration of the use of 1. There are many improvements that can be made. There is no encapsulation function. We will improve it when we need to.


Related articles: