A preliminary study of selenium A preliminary study of selenium

A preliminary study of selenium A preliminary study of selenium

A preliminary study of selenium

Introduction and installation of selenium

Introduction

Selenium is an automated testing library for websites, but because it has a large number of automated libraries and can call browsers, it is often used in crawler technology. It is precisely because it invokes the browser, it has almost become an unsolvable crawler. In the field of neural networks, a large number of data sets are needed. Crawlers are a way to quickly obtain data. This is exactly my motivation for learning this library.

installation

selenium installation

Use pip install -U seleniumto

Diver installation

Selenium needs the corresponding browser driver to call various browsers. I will use chrome to test. After the test is successful, it will be converted to the non-interface PhontomJS. To use Chrome, you need to download the corresponding driver and copy it to the Python installation folder, and use PhontomJS to copy the .exe file directly to the Python installation folder.

Selenium basic operation

Browser operation

Import library

from selenium import webdriver

Open the browser

driver = webdriver.PhantomJS() #Open the PhantomJS browser
driver = webdriver.Chrome() #Open Chrome browser

visit website

driver.get(url)

Visit the website with url, if you use Chrome, you will see the opened browser jump to the specified url

Interactive operation

Get form elements

name_field = driver.find_element_by_id("username")
submit_button = driver.find_element_by_tag_name("button")
submit_button = driver.find_elements_by_link_text("Educational Administration System")

According to the element id, type and hyperlink name to get the element, in addition to the above methods, there are

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

There are also ways to get multiple elements:

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

Enter form data

name_field = driver.find_element_by_id('loginName')
name_field.send_keys('...')

Use the send_keys()method to input data after getting the element

Click on a button or hyperlink

submit_button = driver.find_element_by_id('loginAction')
submit_button.click()

Use the click()method to click the button after getting the element

Reference: https://cloud.tencent.com/developer/article/1110633 A preliminary study of selenium A preliminary study of selenium-Cloud + Community-Tencent Cloud