Use python3 + selenium achieve page automatic filling function under Windows

  This article by a blogger ( SunboyL ) original, reproduced please indicate the source: https://www.cnblogs.com/SunboyL/p/11563345.html

  Because of work reasons, the data needs to be entered into xls files online. Because too much amount of data, but also need to spend time to time in the future, Ctrl + C, Ctrl + V is not a permanent solution. Therefore, the initiation of an idea, write a tool for data entry xls files to the site .

  During this time I was beginning to understand HTML, JavaScript, many things are just a little preliminary knowledge. Write more in detail, if you're like me, you believe the article is useful. If the old bird, then skip it ~

  Xls write partially omitted.

  To achieve fill out the form, you need:

  1.python of selenium library. System understanding selenium, go to: http://www.testclass.net/selenium_python

  2.Chrome browser, you can also use other browsers, Reference: http://www.testclass.net/selenium_python/selenium3-browser-driver

  3. Google and browser version corresponding chromedriver, Download: https://sites.google.com/a/chromium.org/chromedriver/home , not open to use taobao alternate address: http://npm.taobao .org / mirrors / chromedriver /

First, configure the chromedriver

  1) first check the version number of Chrome browser itself, to the chromedriver download sites to find the Chrome browser with its own matching chromedriver version download. I downloaded the win32 version, as shown in detailed steps:

 

 

  2) must extract and copy to the chromedriver.exe Chrome browser directory, shown in detail in FIG steps:

  

 

  3) For convenience, we can also add Chrome to the system path environment variable:

  

 

  This, chromedriver on the configuration finished it.

 

Two, Python install selenium library

  To complete the installation using the following command:

  

pip3 install selenium

  Figure:

  

 

Third, the test selenium library

  Edit the following code to achieve a simple function to open the Baidu page and search for "cnblogs" the (follow-up to explain the source code of id):

Import OS
 from Selenium Import the webdriver 


DEF Test (): 
    Driver = webdriver.Chrome ( " chromedriver.exe " )   # chromedriver path where 
    driver.get (R & lt " http://www.baidu.com " ) 

    driver.find_element_by_id ( " kW " ) .send_keys ( " cnblogs " )   # input cnblogs 
    driver.find_element_by_id ( " su " ) .click ()   # click" Baidu, "the search 


IF  __name__ =="__main__":
    test()
    os.system("pause")

  

  The test is complete, then we can use.

  

  

Four, selenium use

   selenium provides several methods of positioning of page elements, such as by id, name, classname, xpath variety of ways. For details, see: http://www.testclass.net/selenium_python/find-element/ , here it is not a repeat.

  This, we had almost all prepared to do a good job of filling out the form you, the last step is to locate specific elements of our website and to achieve the goal of filling.

 

V. positioning page elements

  chrome developer tools provides us with a way of positioning page elements very traversal. We locate Baidu home page text input box and the "Baidu it" button as an example:

  1. Open the Chrome browser, press F12 to open the Developer Tools. Click into the "Elements" section, we will be able to see the full page HTML code. Figure:

  

 

 

  2. Click the Developer Tools upper-left corner of the small button to locate the page elements, we locate the text entry box to find the input box corresponding element id, of course, we can also be located by other values, as shown:   

       

 

  Next we locate Baidu button:

    

 

   From here we can see, id text input box is "kw", Baidu, id button is su, with the above information, we can write the code point of the third largest.

 

Sixth, some of the problems encountered in practice: an exception is thrown when handling bulk filling : selenium.common.exceptions. ElementClickInterceptedException: the Message: the Click intercepted Element

  For fill-volume data, we often need to click the button to increase the fill area. In the example below

  

  Through Chrome Developer Tools, we locate the button element, three buttons belong to the same class, class = "addbutton":

  

 

   Suppose we have diverted to locate elements row_element, then we begin to implement bar code:

  1) First Edition Code

    We positioned by row_element to the second addbutton button and click on the following code:

  

  In the above manner, we realized the logic automatically add a line of filling area.

  But it did not, when I batch filling this way, the way to fill in a form throws an exception:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="button" tabindex="-1" class="addbutton" value="+↓" onclick="postInsert(11)"> is not clickable at point (737, 833). Other element would receive the click: <iframe frameborder="0" name="hiddenwin" id="hiddenwin" class="debugwin"></iframe>
(Session info: chrome=77.0.3865.90)

  

  Probably it means that other elements are intercepted. You can only find a new approach.

  All kinds of information search, the results appeared in the second edition of the code.

  2) Second Edition Code

  The first edition of the Code with some slight modifications, using JavaScript script execution, as shown:

  

  

print("添加一条填表区...")
add_buttons = row_element.find_elements_by_class_name('addbutton')
# add_buttons[1].click()
driver.execute_script("arguments[0].click()", add_buttons[1])
View Code

  After modification, the error just never occurred -

  3) Third Edition codes , further improvement

  With its own more familiar with JavaScript and HTLM, and looked again find themselves around a big bend , in fact, there is an easier way to achieve.

  Author of the original logic is positioned by various means to the line element, then target specific button, and then click () will achieve.

  But we look back HTLM source, as follows:

  

  

  We can see that, in fact, the click event is in response to a postInsert () function !

  We directly driver.execute_script () call postInsert () function can be, completely no need to locate elements position ah, do not! need! Positioning! Ah! Ah!

  postInsert () function accepts an integer value that represents a new row is inserted after the first few lines. Some slight modifications to the code, there will be a third edition of the code, a line to get:

  

 

 At this point, the tool finished, I these days is to understand the basic HTML from 0, JavaScript began, step by step to deepen the understanding and the tools to write, run into the pit, the number does not count them out. But also a great harvest, continue to fuel it!

  

 

Guess you like

Origin www.cnblogs.com/SunboyL/p/11563345.html