Selenium2+python automation 11-locating a group of elements find_elements

foreword    

The previous articles are all about how to locate an element. Sometimes there are multiple objects on a page that need to be manipulated. It is cumbersome to locate them one by one. At this time, a group of objects can be located.

webdriver provides a method for locating a group of elements, which is actually the same as the previous eight positioning methods, except that the front is singular, and here is the plural form: find_elements 

This article takes Baidu search as an example, randomly selects a search result from the search results, and then clicks Check.

1. Positioning search results

    1. After entering 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 attributes.

    2. As you can see from the search results, their parent elements are the same: <h3 class="t">

    3. The tags are the same, and the target attribute is the same: <a target="_blank" 

    4. So here you can use css positioning (of course, it is also possible to use xpath)


2. Confirm the positioning results

    1. The previous positioning strategy is just a conjecture. It may not necessarily obtain the object you want, and it will also locate some unwanted objects.

    2. Then you can get the properties of the object to verify whether the positioning is accurate. Here you can get the href attribute and print out the url address

3. Random function

    1. There are 10 search results, just randomly pick one from these 10

    2. Import the random function first: import random

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

Fourth, open the url randomly

    1. Randomly pick a url address from the returned result

    2. Use the get method to punch in the url

    3. In fact, this method is an interface test, not a UI automation, here is just to open up the mind, it is not recommended to use this method

5. Open by clicking

    1. The previous method is to directly access the url address, which is the category of interface testing. To truly simulate the user's click behavior, the click method must be used.

# 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"测试部落")

driver.find_element_by_id("kw").submit()

s = driver.find_elements_by_css_selector("h3.t>a")

# set random value

t = random.randint(0, 9)

# Take a random result and click the mouse

s[t].click()

 

I don't know if any of my friends have paid attention to a detail. After entering the keyword in the search box, I did not click the search button, but used the submit method, which is equivalent to the Enter key.

The specific operation object method will be introduced in detail in the next chapter. This article mainly learns to locate a group of objects, and then randomly operate one of them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342910&siteId=291194637