The Selenium profile

Selenium is a tool for Web application testing. Selenium tests run directly in the browser, just as real users do. Supported browsers include Internet Explorer (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, Edge, and more.

Generally we use it in automated testing, testing browser compatibility, regression testing, automated presentations, and other functions. Selenium is also used by some crawlers to simulate browser operations, circumventing browser restrictions and completing crawler tasks.

Common use

1. Select a browser

Large and small browsers on the market are supported, only the corresponding website to download the corresponding version of the driver.

webdriver.Firefox
webdriver.FirefoxProfile
webdriver.Chrome
webdriver.ChromeOptions
webdriver.Ie
webdriver.Opera
webdriver.Phantomjs
webdriver.Remote
webdriver.DesiredCapabilities
webdriver.ActionChains
webdriver.TouchActions
webdriver.Proxy
Copy the code

The following shows how to install the Chrome Driver locally

from selenium import webdriver

If there is no corresponding driver in the environment variable, specify the location of the driver
driver = webdriver.Chrome(executable_path="./chromedriver")
Copy the code

2. Page operations

# open the specified URL
driver.get("https://juejin.cn/")

# Refresh page
driver.refresh()

Get the page title
driver.title    # Django REST Framework complete

Get the current page handler
driver.current_window_handle    # CDwindow-0D939ADC119E1268DB3179FC5EFE0818

# Switch the page parameter to the corresponding window Handle
driver.switch_to.window('CDwindow-52F96D9038F071C723A12B4DF1F6FBC9')

The current page address
driver.current_url

Get page cookie
driver.get_cookies()

# set the cookiedriver.add_cookie({"foo": "bar"})

# Exit page
driver.close()
Copy the code

3. Locate elements

There are multiple selectors to locate elements


class By(object) :
    """ Set of supported locator strategies. """
    # id selector
    ID = "id"
    # xpath selector
    XPATH = "xpath"
    # link text selector
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
   
    CLASS_NAME = "class name"
    # CSS selector
    CSS_SELECTOR = "css selector"
Copy the code

Quickly get different selector parameters for page elements (Google browser can quickly generate selector parameters)

# XPATH
ipt = driver.find_element(By.XPATH, '//input[@type="search"]')

# CSS_SELECTOR
ipt = driver.find_element(By.CSS_SELECTOR, '#juejin > div.view-container.container > div > header > div > nav > ul > li.search-add > ul > li.nav-item.search > form  > input')

Copy the code

4. Element operations

After locating the page element, you can get the attribute of the element and the node for the operation

ipt = driver.find_element(By.XPATH, '//input[@type="search"]')

# input Input box to enter data
ipt.send_keys("Django REST Framework complete")

Get the attribute value of the node
ipt.get_attribute("value")    # Django REST Framework complete

# input search
ipt.send_keys(Keys.ENTER)

# Submit data
ipt.submit()

Copy the code

4. Perform the action

mark = driver.find_element(By.XPATH, F '//mark[text()="Django REST Framework completed "]')

# Action chain move to positioned element -> click on this element
ActionChains(driver).move_to_element(mark).click().perform()
Copy the code

Other common page operations

  • click_and_holdClick on the element and hold
  • context_clickClick on the text of the element
  • double_clickDouble-click on the element
  • drag_and_dropHold the element and drag it to the target element
  • releaseRelease of elements
  • clickClick on the element
  • move_by_offsetMoves elements based on the given parameters

The preceding operations are the same as mouse operations in the browser, and multiple operations can be combined

Find the page div element -> click on the element and hold -> drag the element 10 pixels to the right -> release the mouse
div = driver.find_element(By.XPATH, '//div')
ActionChains(driver).click_and_hold(div).perform()
ActionChains(driver).move_by_offset(xoffset=10, yoffset=0).perform()
ActionChains(driver).release(on_element=div).perform()

Copy the code

5. Special keyboard keys

The following lists the common key input modes


ipt = driver.find_element(By.XPATH, '//input[@type="search"]')

# abdication key
ipt.send_keys(Keys.BACK_SPACE)
# the blank space key
ipt.send_keys(Keys.SPACE)
# CONTROL + A
ipt.send_keys(Keys.CONTROL, 'a')
# CONTROL + C (copy)
ipt.send_keys(Keys.CONTROL, 'c')
# CONTROL + V paste
ipt.send_keys(Keys.CONTROL, 'v')
# the enter key
ipt.send_keys(Keys.ENTER)
Copy the code

Matters needing attention

The version of Chrome Driver you download needs to be the same as the version of Chrome you have installed on your computer.

For example: My Mac version is 91.0.4472.114 (official version) (x86_64)

The Chromedriver to be downloaded is:

When selecting other browser drivers, pay attention to the driver version. It is best to test whether the browser can be directly opened locally after downloading the driver.

The resources

  • Download Chromedriver
  • Chromedriver
  • Selenium Chinese Documentation