Common WebDriver operations

1 Browser operation 2 Window and pop-up box operation 3 Cookies operation 4 Locating simple objects 5 Page elements operation 6 Mouse events 7 Keyboard events


\

1 Browser Operations

# properties:
driver.current_url Get the URL of the current page
driver.title Get the title of the current page
driver.page_source Get the HTML source code for the page
driver.port The port used to get the browser
driver.capabilities['version']  Print the browser version value

# browser:
driver.get(url):# the browser loads the URL
driver.back() # Browser Back
driver.forward() # browser Forward
driver.refresh() # Browser refresh (click refresh button)
driver.set_page_load_timeout(5) # set the page load time, if the timeout will run exceptionDriver. Implicitly_wait (seconds)Implicit wait: wait a certain amount of time for an element to load on the page.
If the element is located ahead of time, continue execution. If the load is not loaded within 10 seconds, NoSuchElementException will be thrown.


# execute js:
driver.execute_script(js) # call js
Copy the code

\

2 Window and pop-up operations

# window:
driver.current_window_handle  Get the handle to the current window
driver.window_handles  Get all window handles

driver.maximize_window()  # Maximize browser display
driver.set_window_size(480.800)  Set the browser width to 480 and height to 800
driver.get_window_size()  Get the length and width of the current window
driver.get_window_position()  Get the current window coordinates
driver.set_window_position(300.200)  # set the current window coordinates
driver.get_screenshot_as_file(filename)  # intercept the current window
# instance: driver. Get_screenshot_as_file (' D: / selenium/image/baidu. JPG ')

driver.close()  Close the current window, or the last window to open
driver.quit()  Close all associated Windows and safely close the session

# Window switch:
driver.switch_to_frame(idOr the name attribute value)Switch to the new form (same window). If there is no ID or attribute value, xpath can be used to locate the iframe and pass the value to switch_to_frame()
driver.switch_to.parent_content()# exit the current level 1 form. This method corresponds by default to the nearest switch_to.frame() method
driver.switch_to.default_content() Jump back to the outermost pageDriver.switch_to_window (window handle)# Switch to a new windowDriver.switch_to.window (window handle)# Switch to a new window

# Popbox switch:
driver.switch_to_alert() # Warning box processing. Handles alerts, confirms, and prompts generated by JavaScript
driver.switch_to.alert() # Warning box processing
Copy the code

\

3 cookies operation

driver.get_cookies()   Get all cookies of the current session
driver.get_cookie(cookie_name)  Cookie_name = 'cookie_name';
Driver.get_cookie ("NET_SessionId")

driver.add_cookie(cookie_dict)   # add cookie. "Cookie_dict" refers to a dictionary object and must have name and value values
driver.delete_cookie(name,optionsString)  Delete cookie information
driver.delete_all_cookies()  Delete all cookie information
Copy the code

\

4 Locating simple objects

If you can locate a unique attribute by id or name, try not to use xpath or CSS ID to locate a unique attribute.

diver.find_element("xpath".".//a//span") # Good for encapsulation
 
driver.find_element_by_id()
driver.find_element_by_name()
driver.find_element_by_class_name()
driver.find_element_by_tag_name()
driver.find_element_by_link_text()
driver.find_element_by_partial_link_text()  # fuzzy query
driver.find_element_by_xpath()
driver.find_element_by_css_selector()  # CSS select locator
Copy the code

\

5 Page element operations

# properties:
element.size  Get the size of the element.
element.text   Get the text of the element.
element.tag_name   Get the tag name

element.clear()  Clear the default content of the input box
element.send_keys("xx")  # used to enter xx content in an input field
element.click()  Use to click a button
element.submit()  # submit form
element.size  # return the size of the element
element.text  Get the element text
element.get_attribute('value')
# return the value of the element's attribute, which can be id, name, type, or any other attribute the element owns
If the value is input, the current input value can be obtained by fetching value

element.is_displayed ()
# return whether the result of the element is visible, True or False

element.is_enabled()  Check whether the element is available
element.is_selected()   # return whether the radio button, check box element result is selected (True or False)
element.value_of_css_property(height)  Get element CSS style attributes
Copy the code

\

6 Mouse Events

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

mouse =driver.find_element_by_xpath("xx") # Position the mouse element

# Perform mouse action on the located element
ActionChains(driver).context_click(mouse).perform() # Right-click operation
ActionChains(driver).double_click(mouse).perform() # Double-click the mouse
ActionChains(driver).move_to_element(mouse).perform() # Mouse move to the top operation
ActionChains(driver).click_and_hold(mouse).perform() # Left mouse button down operation
ActionChains(driver).release(mouse).perform()  # Mouse release

# mouse drag
element = driver.find_element_by_name("xxx")  # Locate the original position of the element
target = driver.find_element_by_name("xxx") Locate the target location to which the element will be moved
ActionChains(driver).drag_and_drop(element, target).perform() Move the element
Copy the code

\

7 Keyboard Events

# introduce the Keys package
from selenium.webdriver.common.keys import Keys

element.send_keys(Keys.BACK_SPACE)  # Delete key (BackSpace)
element.send_keys(Keys.SPACE)  # Space
element.send_keys(Keys.TAB)  # Table key (Tab)
element.send_keys(Keys.ESCAPE)  # Back key (Esc)
element.send_keys(Keys.ENTER)  # Enter
element.send_keys(Keys.CONTROL,'a')  # Select all (Ctrl+A)
element.send_keys(Keys.CONTROL,'c')  # Copy (Ctrl+C)
element.send_keys(Keys.CONTROL,'x')  # Cut (Ctrl+X)
element.send_keys(Keys.CONTROL,'v')  # Paste (Ctrl+V)
element.send_keys(Keys.F12)   # keyboard F12.

# Enter space bar + "python"
element.send_keys(Keys.SPACE)
element.send_keys("python")
Copy the code