This article is participating in Python Theme Month. See the link for details

This blog focuses on Selenium common methods and properties, along with mouse hovering and Select operations

@[TOC](Common Methods and Properties of Selenium)

Selenium common methods and properties

Methods and attributes are the content of objects, and this blog focuses on Selenium objects and captured Web element objects.

Maximize_window method

This method maximizes the browser window as follows:

# browser maximization
driver.maximize_window()
# Find the input box by ID and enter the model
driver.find_element_by_id("kw").send_keys("Model")
Copy the code

The refresh method

Refresh the page, similar to F5 or CTRL+F5.

driver.refresh()
Copy the code

The back way

The browser returns to the previous page. Try it yourself.

The forward method

Browser forward one page operation, specific try yourself.

The quit way

Close the browser and kill the browser process. Note that different browsers have different driver processes. For example, I use Firefox, so I use Firefox.

driver.quit()
# driver.close()
Copy the code

The difference between this method and close is that closing the browser kills the process while close does not.

All of the above are methods of the driver object, and I’ll explain the attributes of the driver object.

Current_window_handle properties

Get the browser window handle, just look, use a few scenarios. I’m showing 21 locally, you can try your computer output.

# Open Baidu Pictures
driver.get('https://image.baidu.com/')

print(driver.current_window_handle)
Copy the code

Current_url properties

Get the url of the current window, the code tests itself.

The title attribute

Gets the title title value for the current page.

The driver attribute values are briefly described in three ways, followed by the methods and attributes associated with the element objects that Selenium has obtained.

Send_keys method

The previous blog has been used, on behalf of keyboard input, such as baidu picture input text.

from selenium import webdriver
import time

driver = webdriver.Firefox()

# Open Baidu Pictures
driver.get('https://image.baidu.com/')

# Find the input box by ID and enter the model
driver.find_element_by_id("kw").send_keys("Model")
Copy the code

Get_attribute method

Gets the attribute value of the specified tag. Gets the specified tag and then the specified attribute of the tag.

# Open Baidu Pictures
driver.get('https://image.baidu.com/')

print(driver.find_element_by_id("kw").get_attribute("class"))
Copy the code

Is_selected method

Determines whether an element is selected. This is typically used for checkboxes, such as the user protocol shown below. Returns True if checked, False otherwise.

Is_enable and is_displayed methods

The is_Enable method is used to determine whether the retrieved element is available, and the is_displayed method is used to determine whether the element is displayed on the page.

Clear method

Clear the contents of the input box.

The text property

The text value of an element can be obtained through the text attribute of the element object.

driver.get('https://www.csdn.net/')
print(driver.find_element_by_class_name("headP").text)
Copy the code

Selenium allows you to select elements after you hover

In Selenium, all keyboard and mouse operations are wrapped in Action Chains class. You need to import this class to use.

Some common methods in Action Chains are as follows:

  • Click (an element)
  • Click_and_hold (an element) Click and hold, such as drag and drop
  • Double_click (an element) double-click
  • Context_click (an element) Executes the right mouse click on the element
  • Drag_and_drop (element, drag to element) Mouse drag to drag the target element onto the specified element
  • Drag_and_drop_by_offset (element, x coordinate, y coordinate) drag to the specified position
  • Key_down () Holds down the key on the keyboard
  • Key_up () releases the keys on the keyboard
  • Move_to_element Move the mouse over the element
  • Move_to_element_with_offset (element, x coordinate, y coordinate) moves an element to the specified position
  • Perform () implements a series of Action Chains
  • Release (element) releases the mouse

Next, open the CSDN home page, then move the mouse over the message and click the announcement link that appears.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open the home page
driver.get('https://www.csdn.net/')
Get the message link by id
head = driver.find_element_by_id("toolbar-remind")
# Simulate mouse movement over the message
ActionChains(driver).move_to_element(head).perform()
# Click the announcement link
driver.find_element_by_link_text("Notice").click()
Copy the code

More content you can dig, very interesting.

Select the operating

Select is simply an operation on a drop-down list in a Web page, which is a bit special, so the Selenium library writes a separate class. Note the import of the Select class.

from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
Open the registration page of xuexin
driver.get('https://account.chsi.com.cn/account/preregister.action?from=chsi-home')

# Find id dropdown list

credentialtype = driver.find_element_by_id("credentialtype")

Select(credentialtype).select_by_index(1)
Copy the code

After operation, the successful choice of Hong Kong, Macao and Taiwan id card.

Select_by_index (select_by_value); select_by_index (select_by_value); There is also a method called select_BY_visible_TEXT that relies on what is displayed.

In addition to setting which drop-down list item to select, you can also obtain all drop-down list options. Pass three properties, the first being options, code example:

# Find id dropdown list
credentialtype = driver.find_element_by_id("credentialtype")
options = Select(credentialtype).options
print(options)
Copy the code

The second is all_selected_options, which returns the selected option from the drop-down list. The third, first_selected_option, returns the first selected option.

The second and third try by yourself.

Write in the back

This blog mainly introduces the methods and properties commonly used in Selenium, and extends the methods in Action Chains class that implement mouse hover based on the methods in Select class. You will have a deeper understanding of Selenium after mastering it.