2.8 Locate a set of elements elements

Preface     
The previous articles are all about how to locate an element , Sometimes there are multiple objects on a page that need to be manipulated , If we go to the location one by one , More complicated , In this case, you can locate a group of objects .
webdriver Provides a way to locate a set of elements , It's the same as the previous eight positioning methods , It's just the singular ahead , Here is the plural :find_elements

This article takes Baidu search as a case , Randomly select a search result from the search results , Then click to see .

One 、 Locate search results
    1. Enter keywords in Baidu search box “ Test tribe ” after , use firebug View page elements , You can see that these search results have common properties .

2. As you can see from the results of the search , They have the same parent element :<h3 class="t">
    3. The labels are all the same , And target Properties are the same :<a target="_blank" />
    4. So here you can use css location ( Of course with xpath It's OK, too )

Two 、 Confirm the positioning results
    1. The previous positioning strategy is just a conjecture , You don't have to get what you want , You can also locate unwanted objects .
    2. So you can get the properties of the object , To verify that the positioning is accurate . Here you can get href attribute , Print out url Address .

3、 ... and 、 Random function
    1. The search results are 10 strip , From here 10 Take a random one from the bar ok 了
    2. Let's import random functions first :import random
    3. Set the random value range to 0~9:a=random.randint(0~9)

Four 、 Random open url
    1. Take one at random from the returned results url Address
    2. adopt get How to punch in url
    3. In fact, this way is interface testing , Do not belong to UI automation , It's just an open mind , This method is not recommended

5、 ... and 、 adopt click Click on the open
    1. The previous method , It's a direct visit url Address , It's interface testing , Truly simulate user click behavior , Have to use click Methods

# 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)
# Take a random result and click the mouse
s[t].click()

I don't know if any of my friends have paid attention to one detail , Before entering keywords in the search box , I didn't click the search button , But with submit Methods ,submit It's equivalent to the Enter key .
Specific operation object method , The next part introduces in detail . This article focuses on learning to locate a group of objects , And then randomly manipulate one of them .

2.8 Locate a set of elements elements More articles about

  1. Selenium2 Study ( 7、 ... and )-- Locate a set of elements find_elements

    Preface The previous articles are all about how to locate an element , Sometimes there are multiple objects on a page that need to be manipulated , If we go to the location one by one , More complicated , In this case, you can locate a group of objects . webdriver Provides a way to locate a set of elements , Follow the previous eight positioning methods ...

  2. seleniumu 3.0 Check box operation ( Locate a set of elements )

    General verification check box can choose to click to locate a group of elements to loop through and execute click events . But sometimes there may be different results in different browsers . At present, Google browser often has multiple clicks on the same check box , As a result, the last two check boxes are not checked ...

  3. Python+selenium( Locate a set of elements )

    We know it well Webdriver Yes 8 A way to locate elements , But when you need to locate a set of elements , have access to Webdriver The corresponding 8 A method for locating a set of elements , Namely : find_elements_by_id() ...

  4. webdriervAPI( Locate a set of elements )

    By locating a set of elements to , To check the options you need to check . from selenium import webdriver from selenium.common.exceptions import NoS ...

  5. Selenium 2 Automation test practice 14( Locate a set of elements )

    One . Locate a set of elements WebDriver It also provides the corresponding 8 The method a clock uses to locate a set of elements . The way to locate a group of elements is similar to the way to locate a single element , The only difference is in the words element There's another one in the back S In the plural . Locate a set of elements ...

  6. Selenium Locate a set of elements

    up to now , We already know how to locate an element , Sometimes we need to get multiple elements on the page , What to do ? Is it possible to locate one by one , That's too much lower Is that right . webdriver Provides a way to locate a set of elements , With the eight mentioned above ...

  7. webdriver(python) Study note 4 —— Locate a set of elements

    webdriver It's easy to use find_element Method to locate a specific object , But sometimes we need to locate a set of objects , You need to use find_elements Method . Locating a set of objects is generally used in the following scenarios : ...

  8. python selenum --- How to locate a set of elements

    Use findElements Method to locate a set of objects Locating a set of objects is generally used in the following scenarios : ·  Batch operation objects , For example, all the checkbox All hooked ·  First get a set of objects , Then filter out some objects that need to be located in this group of objects ...

  9. selenium--python How to locate a set of elements and return text values

    from selenium import webdriverimport time a=[] # Create an empty list to store the queried element groups driver = webdriver.Firefox()driver ...

Random recommendation

  1. Six principles of design pattern (5)&mdash; Dimitar's law

    Definition : An object should have the least knowledge of other objects . Dimitri's law is also called the law of least knowledge , English full name Least Knowledge Principle , Referred to as LKP. Personal understanding : The main purpose of dimitt's law is to decouple between classes , ...

  2. The smallest K Number : Use the idea of fast scheduling to solve related problems

    The key to implement the quick sort algorithm is to select a number in the array first , Next, divide the numbers in the array into two parts , The number smaller than the selected number moves to the left of the array , The number larger than the selected number moves to the right of the array . This function can be implemented as follows : int Partit ...

  3. DevOps Efficient shell command

    1. View the top Information We all know how to use top To see the real-time indicators of the system , stay Linux Server , If you want to see the top real-time information ( With node Process, for example ), You can use a command like this : top -p `pgr ...

  4. C# operation Cookie

    /* The creator : The blog of kitchen knife Jushi  * Date of creation : 2014 year 09 month 02 Number  * function : operation Cookie  *  */ namespace Net.String.ConsoleApplication { ...

  5. yield return and yield break

    This is still a little interesting , Both have the meaning of return , But what's the difference ? 1.return Will destroy the local variables of the function , The next call will generate a new value 2.yield When you exit the function , Variables exist naturally , The next time the function is called, the variable ...

  6. TensorFlow tf.gradients Detailed analysis of the usage and specific examples

    tf.gradients The official definition of : tf.gradients( ys, xs, grad_ys=None, name='gradients', stop_gradients=None, ) Cons ...

  7. spring Learn more ( Four )-----spring aop

    AOP summary aop It's actually aspect oriented programming , for instance , For example, there are n One way is to provide http Service , So if I need to know about these http Service response time monitoring , The traditional way is to add logic to each method , But these ...

  8. OGR The superposition function in space Union

    stay OGRLayer Is called in Union Function . Have a look first Union Prototypes of functions : Union(OGRLayer *pLayerMethod, OGRLayer *pLayerResult, char * ...

  9. WPF Demo511 Control sharing Events

    Routing events : 1. The three strategies commonly used for routing events are as follows : A.Bubble( Bubble mode ): Events are fired by themselves and passed on to the root element ; B.Direct( Direct mode ): Only the event source has the opportunity to respond to the event ( Like traditional events ); C. ...

  10. TensorBoard practice 1

    When I look up the map again , Delete the old logs/ The following file tf.scalar_summary('loss',self.loss) AttributeError: 'module' object has no a ...