Web Automation-Select Action Element 1

Source: Internet
Author: User
Tags tag name

Article Turn Confessions Black Feather teaches Python

The automation of all UI (user interface) operations requires the selection of interface elements.

Select the interface element: first let the program find the interface element you want to manipulate.

The element is found before the element can be manipulated.

Methods for selecting elements

How can the program find the Web interface elements to manipulate?

The method is to choose according to the characteristics of this web element.

How are the characteristics of the elements viewed?

You can use the browser's Developer toolbar to help us view and select Web elements.

Please install the latest version of the Chrome browser (can baidu search download).

Visit Baidu with Chrome browser, press F12, click the Elements tab at the arrow to see the HTML element of the page

Then, click the leftmost icon, as shown below

After that, you can view the HTML tag content of the element by clicking on the element in the interface.

For example, the front of the image of the highlight, is the Baidu Search input box corresponding to the element input.

Select an element based on the element's ID attribute

If you look closely at the input element above, you will find that it has a property called ID.

We can think of the ID as the number of the element, which is used to mark the element in the HTML. According to the specification, if the element has an ID, the ID must be unique in the current HTML.

So if the element has an ID, selecting the element based on the ID is the simplest and most efficient way.

The following code is to use selenium to access Baidu, and in the input box search 黑羽魔巫宗 .

Everyone can run a look.

FromSeleniumImportWebdriver# Create an Webdriver instance object that indicates the use of Chrome browser driverDriver=Webdriverchrome (r ' D:\webdrivers\chromedriver.exe ' ) The Get method of the span class= "C1" ># Webdriver instance object allows the browser to open the specified URL driver. ( ' https://www.baidu.com ' ) # select elements by ID, Returns the element corresponding to the Webelement object element = driver. Find_element_by_id ( ' kw ' ) # through the Webelement object, You can manipulate the page elements # such as input string into this input box element. Send_keys ( Black feather Huzong \n ' )   

which

driver = webdriver.Chrome(r‘d:\webdrivers\chromedriver.exe‘)

The Webdriver instance object is returned, and we can manipulate the browser through this instance object, such as opening the URL, selecting the interface element, and so on.

The following code

driver.find_element_by_id(‘kw‘)

Is the method of using Webdriver instance object find_element_by_id, this method is to select the element according to the ID value of the input box element ' kw '.

After selecting the element, the Find_element_by_id method returns a webelement, which allows us to manipulate the corresponding interface element.

For example, the Send_keys method is to enter a string in the corresponding element,

And the click Method can click on the element.

Select elements based on class attribute, tag name

In addition to the element's ID, we can also select elements based on the class attribute of the element.

Please visit this URL http://www. Python3.vip/doc/tutorial/python/code/sample1.html

This URL corresponds to the HTML content of the following sections

<! DOCTYPE html><HtmlLang="EN"><Head><MetaCharSet="UTF-8"><Title> White Moon Black Feather test Page 1</Title><Style>. Animal{Color:Red;}</Style></Head><Body><DivClass="Plant"><Span> Potatoes</Span></Div><DivClass="Plant"><Span> Onions</Span></Div><DivClass="Plant"><Span> Cabbage</Span></Div><DivClass="Animal"><Span> Lions</Span></div> <div class= "animal" ><span > Tiger </span></div< Span class= "P" >> <div class=  "animal" ><span> goat  </span></div>  </body></html>   

All plant elements have a class attribute value of plant.

All animal elements have a class attribute value of animal.

If we want to select all the animals, we can use the methodfind_elements_by_class_name

driver.find_elements_by_class_name(‘animal‘)

Attention

The Find_elements_by_class_name method returns all elements found to match the criteria (there are 3 elements) and is returned in a list.

And if we use find_element_by_class_name (note less an S) method, only the first element is returned.

Everyone can run the following code to see.

FromSeleniumImportWebdriver# Create an Webdriver instance object that indicates the use of Chrome browser driverDriver=Webdriver.Chrome(R ' D:\webdrivers\chromedriver.exe ')# The Get method of the Webdriver instance object allows the browser to open the specified URLdriver. Get ( ' http://www.python3.vip/doc/tutorial/python/code/sample1.html ' ) # Select element according to class name, return a list # inside is the class attribute value of the element corresponding to animal Webelement object elements = driver. Find_elements_by_class_name ( ' animal ' ) # Remove each Webelement object in the list and print out the value of its Text property # the Text property is the content of the Webelement object corresponding to the contents of the Web page for element in elements: print  (element. Text)              

If we put

elements = driver.find_elements_by_class_name(‘animal‘)

Remove one S, instead

element = driver.find_element_by_class_name(‘animal‘) print(element.text)

Then the first element that returns the class attribute is animal, which is the element

<div class="animal"><span>狮子</span></div>



Similarly, we can find_elements_by_tag_name select all of the tag names as div elements by means of the method, as shown below

FromSeleniumImportWebdriver# Create an Webdriver instance object that indicates the use of Chrome browser driverDriver=Webdriver.Chrome(R ' D:\webdrivers\chromedriver.exe ')# The Get method of the Webdriver instance object allows the browser to open the specified URLdriver. Get ( ' http://www.python3.vip/doc/tutorial/python/code/sample1.html ' ) # Select element according to tag name, return a list # inside is a tag named div of the element corresponding to the Webelement object elements = driver. Find_elements_by_tag_name ( ' div ' ) # take out each of the list The Webelement object, which prints out the value of its Text property # the Text property is the contents of the Webelement object corresponding to the content in the Web page for element in elements: print (element. Text)              

Wait for interface elements to appear

When we do the Web page operation, some element content can not appear immediately, may wait for a period of time.

For example, Baidu search a word, we click Search, the browser needs to send this search request to Baidu Server, Baidu Server processing, the search results back to us.

Just usually Baidu server processing relatively fast, we feel as if the search results appear immediately.

Baidu search for each result corresponding interface element whose ID is the number 1, 2, 3, 4 ...

As follows

Then we can try to use the following code to print out the text content of the first search result

FromSeleniumImportWebdriverDriver=Webdriver.Chrome(R ' D:\webdrivers\chromedriver.exe ')Driver.Get( ' https://www.baidu.com ' ) element = driver. Find_element_by_id ( ' kw ' ) element send_keys ( Black feather Huzong \n) # the element with ID 1 is the first search result element =  Driver. Find_element_by_id ( ' 1 ' ) # print out a text string for the first search result Span class= "K" >print  (element. Text)               

If you go to run it, you will find the following exception thrown

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"1"}

NoSuchElementExceptionThis means that the element cannot be found on the current page, that is, the element with ID 1 is not found.

Why is it?

Because our code executes faster than the Baidu server responds.

Baidu has not had time to return the search results, we executed the following code

element = driver.find_element_by_id(‘1‘)

In that brief moment, the page is not using the ID 1 element (because there is no search results yet). It is natural to report that an element with error ID 1 does not exist.

So how do we solve this problem?

Many smart readers can think of, after clicking on the search, with sleep to wait a few seconds, and so on after the Baidu server returned results, then select the ID 1 element, like this

FromSeleniumImportWebdriverDriver=Webdriver.Chrome(R ' D:\webdrivers\chromedriver.exe ')Driver.Get(' Https://www.baidu.com ')Element=Driver.find_element_by_id( ' kw ' ) element. Send_keys ( ' black feather Huzong \n ' ) # wait 2 seconds from time import sleep< span class= "n" >sleep (2) # 2 seconds later, search again element = driver. Find_element_by_id ( ' 1 ' ) # print out a text string for the first search result Span class= "K" >print  (element. Text)               

We can run a bit, basically is can, no more error.

But there is a big problem with this approach, that is, how long will it be appropriate to wait?

This time the Baidu site reaction may be relatively fast, we wait for a second on it.

But who knows if his reaction is going to be so fast next time? Baidu has also had a server paralysis of things.

Perhaps some readers say, I simply sleep a long time, wait 20 seconds, it is OK?

This also has a big problem, if an automated program needs to wait 10 times, it will take 200 seconds. And perhaps most of the time, the server is reflected very quickly, there is no need to wait 20 seconds, which caused a lot of time wasted.

Selenium provides a more reasonable solution, which is this:

When an element is found not found, it does not immediately throw an exception that cannot be found.

Instead, the element is re-searched periodically (every half a second) until the element is found,

or exceeding the specified maximum wait length, the exception is thrown (if it is an find_elements empty list).

Selenium's Webdriver object has a method calledimplicitly_wait

The method accepts a parameter that is the specified maximum wait duration.

If we add the following code

driver.implicitly_wait(10)

Then all subsequent find_element or similar find_elements method calls will take the above policy.

It is not possible to find the element every half a second to look at the interface again until the element is found, or the maximum length of 10 seconds.

In this way, the final code of our Baidu search example is as follows

FromSeleniumImportWebdriverDriver=Webdriver.Chrome()# Set maximum wait time to 10 secondsDriver.Implicitly_wait(10)driver. Get ( ' https://www.baidu.com ' ) element = driver. Find_element_by_id ( ' kw ' ) element send_keys ( Black feather Huzong \n) element = driver. Find_element_by_id ( ' 1 ' ) print  ( element. Text)               

If you run it again, you can see that there are no errors.

Visit the original website to see the next Operation Web interface 2

Article Turn Confessions Black Feather teaches Python

Web Automation-Select Action Element 1

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.