selenium notes arrangement

Posted by freewholly on Fri, 25 Oct 2019 23:32:07 +0200

Common methods of scattered

 

from selenium import webdriver

# Keys package to be introduced when calling keyboard key operation
from selenium.webdriver.common.keys import Keys

# Create browser object by calling PhantomJS browser specified by environment variable
driver = webdriver.PhantomJS()

# If the phantom JS location is not specified in the environment variable
# driver = webdriver.PhantomJS(executable_path="./phantomjs"))

#Get the url of the current page
driver.current_url
# Page forward backward
driver.forward()     #Forward
driver.back()        # Back off

#Close page
driver.close()  Close current page
driver.quit()  Exit the entire browser

# Take a new page snapshot
driver.save_screenshot("I love learning..png")

# Print the source code after rendering the web page
print driver.page_source

# Get the text content of the id tag with the page name wrapper
data = driver.find_element_by_id("wrapper").text


# Print page title
print driver.title

# ctrl+a select all input box content
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')

# ctrl+x cut input box content
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')

# Input box retype
driver.find_element_by_id("kw").send_keys("itcast")

# Simulate Enter
driver.find_element_by_id("su").send_keys(Keys.RETURN)

Element location

from selenium.webdriver.common.by import By
1.adopt id Search for
driver.find_element_by_id('kw')
driver.find_element(By.ID,'kw')

2.Find by class name
cheeses = driver.find_elements_by_class_name("cheese")
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")

3.adopt name Attribute lookup
driver.find_elements_by_name("email")
driver.find_elements(By.NAME,'email')

4.Find by tag name
driver.find_elements_by_tag_name('div')
driver.find_elements(By.TAG_NAME,'kw')

5.adopt xpath Syntax lookup
driver.find_elements_by_xpath("//div")
driver.find_elements(By.XPATH,'//div')

6.adopt css Selector select element

driver.find_elements_by_css_selector("//div")
driver.find_elements(By.CSS_SELECTOR,'//div')

find_element  Get the first element that meets the condition
find_elements Get all elements that meet the conditions

Form element action

1.button 
input[type='submit']
2.checkbox  input[type='checkbox ']
3.select Drop-down list
-----------
input_tag=driver.find_element_by_id('kw')
input_tag.send_keys("onion")  #Fill
input_tag.clear()  #Eliminate
-----------
remember_tag = driver.find_element_by_id('rememberME')
remember_tag.click()  #click 
-------------------
//Select label:
//Select: select element cannot be clicked directly. Because you need to select elements after clicking
<select> </select>

# Import Select class
from selenium.webdriver.support.ui import Select

# Find the tab for name
select_tag = Select(driver.find_element_by_name('status'))

# 
select_tag.select_by_index(1) #Indexes
select_tag.select_by_value("0") #According to the value in value
select_tag.select_by_visible_text("Radio broadcast") #According to text content
select_tag.deselect_all()  #Uncheck all

 

Behavior chain

Sometimes page operations may take many steps, which can be completed by using the mouse's behavior chain.

from selenium.webdriver import ActionChains
 input_tag = driver.find_element_by_id('kw')
 submit_tag = driver.find_element_by_id('su')

actions= ActionChains(driver)
action.move_to_element(input_tag)  #Mobile element
action.send_keys_to_element(input_tag,'onion') 
action.move_to_element(submit_tag)
action.click(submit_tag)
action.perform()


click_and_hold(element) Click without releasing the mouse
context_click(element) Right click
double_click(element)  double-click 

# Drag ac1 to ac2
ac1 = driver.find_element_by_xpath('elementD')
ac2 = driver.find_element_by_xpath('elementE')
ActionChains(driver).drag_and_drop(ac1, ac2).perform()


 

cookie operation

cookie operation
1. Get all cookie
for cookie in driver.get_cookies():
    print "%s -> %s" % (cookie['name'], cookie['value'])

2.delete cookie
# By name
driver.delete_cookie("CookieName")

# all
driver.delete_all_cookies()

3.according to cookie Of key Obtain value

value =driver.get_cookie(key)

Page waiting

Implicit waiting is to wait for a specific time, while explicit waiting is to specify a condition to continue execution until the condition is met


1.Implicit waiting:Wait an exact time before getting unavailable elements 

If the waiting time is not set to 0 s

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10) # Implicit waiting for 10s
driver.get("http://www.xxxxx.com/loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

2.Display wait

Explicit waiting is to specify a condition to continue executing until the condition is established, or to set a maximum time when waiting. If the time is exceeded, an exception will be thrown.

If you do not write parameters, the program will default to 0.5s Call once to see if the element has been generated. If the element already exists, it will be returned immediately.

from selenium import webdriver
from selenium.webdriver.common.by import By
# WebDriverWait library, responsible for circular waiting
from selenium.webdriver.support.ui import WebDriverWait
# Expected conditions class, which is responsible for starting conditions
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("http://www.xxxxx.com/loading")
try:
    # The page continues to loop until id = "mydynamicalelement" appears, waiting for up to 10s
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()


Presence of element loaded
Element to be clickable

 

Page switching


1. Open a new page
Using js code to open a new page
driver.execute_script("windows.open('"+url+"')")
driver.execute_script("windows.open('https://www.baidu.com/')")

2. A browser must have many windows, so we must have a way to switch windows. The way to switch windows is as follows:

driver.switch_to.window("this is window name")
You can also use the window? Handles method to get the operation objects of each window. For example:

for handle in driver.window_handles:
    driver.switch_to_window(handle)

 

Setting agent

selenium set proxy different browsers have different implementation methods

Google browser is as follows

def proxy_test():
    op = webdriver.ChromeOptions()
    op.add_argument('--proxy-server=https://222.189.144.104:4245')
    driver =webdriver.Chrome(options=op)
    driver.get('http://httpbin.org/ip')

 

Official document link

selenium

 

 

 

 

 

 

 

Topics: Selenium Windows snapshot Attribute