Selenium2+python Automation 11-locates a set of elements find_elements

Source: Internet
Author: User

Objective

The previous few are talking about how to locate an element, sometimes a page has multiple objects to operate, if one by one to locate, more cumbersome, this time you can locate a group of objects.

Webdriver provides a way to locate a set of elements, as in the previous eight positioning methods, but the first is the singular, here is the plural form: find_elements

This article takes Baidu search as a case, select a search result randomly from the search result, then click View.

First, locate the search results

1. After you enter the keyword "test tribe" in the Baidu search box, use Firebug to view the page elements, and you can see that these search results have common properties.

2. From the search results you can see that their parent element is the same:

3. Tags are the same, and the target property is the same: <a target= "_blank"

4. So you can use CSS to locate (and of course XPath is also possible)


Second, confirm the positioning results

1. The previous positioning strategy is only a conjecture, and does not necessarily actually get the object you want, but also the line to some unwanted objects.

2. You can then get the properties of the object to verify that the positioning is correct. Here you can get the href attribute, print out the URL address


Three, random function

1. There are 10 search results, and a random one from these 10 is OK.

2. Import the random function first: import randomness

3. Set the random value range to 0~9:a=random.randint (0~9)


Iv. randomly open URLs

1. Randomly fetch a URL address from the returned results

2. Punch the URL through the Get method

3. In fact, this way is the interface test, not UI Automation, here is just open the mind, do not recommend this method


V. Open with Click

1. The previous method, is the direct access to the URL address, is considered the scope of the interface test, the real simulation of user click Behavior, you have to use the click Method

# Coding:utf-8

From selenium import Webdriver

Import Random

Driver = Webdriver. Firefox ()

Driver.get ("https://www.baidu.com")

Driver.implicitly_wait (10)

driver.find_element_by_id ("kw"). Send_keys (U "Test Tribe")

driver.find_element_by_id ("kw"). Submit ()

s = Driver.find_elements_by_css_selector ("H3.t>a")

# Set Random values

t = Random.randint (0, 9)

# randomly take a result click the mouse

S[t].click ()

I do not know a small partner has not noticed a detail, the front in the search box after entering the keyword, I did not go to click the Search button, but with the method of submit, submit equivalent to enter.

The specific Operation object method, the next article detailed introduction. This article mainly learns to locate a group of objects and then randomly manipulate one of them.

Selenium2+python Automation 11-locates a set of elements find_elements

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.