Python3 selenium randomly selects an element of the same type, python3selenium

Source: Internet
Author: User

Python3 selenium randomly selects an element of the same type, python3selenium

Use Cases:

As shown in, sometimes, when we test, we do not select every direction, nor can we select one direction each time. At this time, we need to run the case each time, randomly select a direction for testing

Usage:

 

random.randint()

 

Example:

 

1 #_. _ coding: UTF-8 _. _ 2 3 "" 4: author: Flower Test 5: time: 2017.05.04 6: content: randomly select an element of the same type 7 "8 9 from selenium import webdriver10 import random11 import time12 13 class Course: 14 15 driver = None16 17 def _ init _ (self): 18 self. driver = webdriver. chrome () 19 self. driver. get ('HTTP: // www.kgc.cn/list') 20 self. driver. maximize_window () 21 22 def get_first_category (self): 23 "24 random filter course direction: 25 26: return: 27" 28 first_categories = self. driver. find_elements_by_css_selector ('dl. new-courseTag dd') [0] 29 first_categories = first_categories.find_elements_by_css_selector ('div. new-courseHref A') 30 first_category = first_categories [random. randint (1, len (first_categories)-1)] 31 time. sleep (1) 32 return first_category33 34 def run (self): 35 "36 call the filtering direction of the course, and randomly select 37 38: return: 39 "40 first_category = self. get_first_category () 41 first_category_name = first_category.text42 print ("the randomly selected course direction is: {0 }". format (first_category_name) 43 first_category.click () 44 time. sleep (1) 45 46 self. driver. quit () 47 print ('test passed ') 48 49 if _ name _ =' _ main _ ': 50 course = Course () 51 course. run ()

 

Note:

1. My scripts are based on workshops. If you have any questions, please leave a message. If you have any questions, I will give them a prompt answer.

2. Hard work and originality. for reprinting, please indicate the source

 

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.