The basic operation of web elements
Gets the element
-
Get the elements, variables assignment
-
inputbox = driver.find_element_by_id("kw")
-
-
Gets the element text messages
-
driver.find_element_by_css_selector('[href="http://news.baidu.com"]').text
-
-
Gets the element property values
-
driver.find_element_by_css_selector('[href="http://news.baidu.com"]').get_attribute("class"))
-
- Get more elements
Operating elements
-
Click on
- Click: .click ()
- Double-click: .double_click ()
- Right: .context_click ()
-
Edit box to enter information
-
.send_keys()
-
-
Clear text
-
.clear()
-
Wait elements
- Forced to wait
- time.sleep()
- Unit: s
- Forced to stop running the code, not smart enough in selenium in; If time is short, the elements have not loaded it will lead to error; if set too long, it will affect the efficiency of the code
- Explicit waiting
- WebDriverWait().until()
- Unit: s
- Within a set time, every 0.5s to see whether the element is visible, if visible, terminate wait; if not visible then continue to wait until the timeout error
- Implicit wait
- implicitly_wait()
- Unit: s
- After setting all the elements positioning will wait for a given time, up until the element is present, the elements do not appear within the set time will error
- Implicit wait is a global setting, if you need to change the back of the waiting time, you also need to re-set
Example:
- The use of selenium, open chrome, enter the URL: https: //baidu.com
- View news text message
- Input box, selenium python, click the Search button to print out all the search information
from Selenium Import the webdriver from selenium.webdriver.common.by Import By from selenium.webdriver.support.ui Import WebDriverWait from selenium.webdriver.support Import expected_conditions EC AS Import Time Driver = webdriver.Chrome () driver.get ( " HTTPS: //www.baidu.com " ) # implicit wait # which is global, and after setting, the following methods will find all pre-like 10s, until the element is present driver.implicitly_wait (10 ) # positioned Baidu input element inputbox = driver.find_element_by_id (" KW " ) # news top right corner of the positioning element News = driver.find_element_by_css_selector ( ' [the href = "http://news.baidu.com"] ' ) # explicit wait, and assigned to the element Button Button = WebDriverWait (driver, 10 ) .until ( EC.presence_of_element_located ((By.ID, " SU " )) ) # WebDriverWait analytical parameters: # WebDriverWait (driver, timeout, poll_frequency = POLL_FREQUENCY, ignored_exceptions = None) # driver: driver browser --- [driver = webdriver.Chrome ()] # timeout: set the maximum wait time #poll_frequency: interval detection element once per 0.5s query, the default is 0.5s, may default # ignored_exceptions: error message after the timeout, the default is NoSuchElementException # get news text information Print ( " text messages linked to news : " , news.text) # use get_attribute method to get the news link class attribute value Print ( " News Links of class attribute value: " , news.get_attribute ( " class " )) Print ( ' \ the n-***** ************* divided ****************** \ n- ' ) # Baidu input box, Selenium inputbox.send_keys ( " Selenium " ) # forced to wait for 1s the time.sleep (1 ) # clear the input box contents inputbox.clear () # input Baidu Python input box the Selenium inputbox.send_keys ( " the Selenium Python " ) # click on Baidu button Button.Click () # get query results, all the // div // h3 / a element information # Baidu search, query results with a lot of advertising messages, although the interface plug-ins can be shielded, but there are still elements of information! # By contrast elements non-advertisement information with advertisement information, that, data-landurl property ad elements present in the form of a link attribute value # screened by not (contains (@ data-landurl , 'http')), is not comprising data-landurl attribute value of the presence element http Texts = driver.find_elements_by_xpath ( " // // div H3 / a [Not (the contains (data-landurl @, 'http'))] " ) # using a for loop, listed each element of the text attributes for one in texts: print(one.text) driver.quit()
Output:
News link text messages is: News News Links of class attribute value: mnav ****************** ************** split **** use python reptile Selenium library from entry to give up (eight) of - python spiritual path ... Selenium + one learning Python - a rain shelter - blog Park Selenium (Python web testing tool) basic usage Detailed _python _ script home Python Bindings for the Selenium webdriver - the Selenium · PyPI the Selenium with Python - Python Bindings the Selenium 2 ... automated test entry (based on Python) the Selenium - baby_hua column - CSDN blog the Selenium with Python - Python Bindings the Selenium 2 ... Python uses reptiles basis Selenium library (xxii 01) - Kay resistant blog - CSDN ... 2. Quick Start - Selenium-Python Chinese document 2Documentation the Selenium + Python automated test series (a): Log _ Mu class notes