Selenium for web automation

一、Selenium(http://www.selenium.org/)

  Web automation testing tool. It supports various browsers, including mainstream interface browsers such as Chrome, Safari, Firefox, etc. If you install a Selenium plug-in in these browsers, you can easily implement web interface testing. In other words, Selenium supports these browser drivers

  Simulate browser actions such as form actions, click events, keyboard input

2. Content

  Installation, positioning web elements, mouse operation, keyboard input, window switching, cookie operation, calling js code, window screenshot, file upload, warning box processing, multi-form switching, waiting...

Chrome browser driver: https://sites.google.com/a/chromium.org/chromedriver/downloads

Domestic Alibaba Cloud mirror: https://npm.taobao.org/mirrors/chromedriver

https://sites.google.com/a/chromium.org/chromedriver/

3. Operation

from selenium import webdriver

wd = webdriver.Chrome() # create an object

1. Basic operation

wd.get('http://baidu.com') # Open a web page

title = wd.title # Get the page title

print(title) # print output

wd.set_window_size(400,800) # Set the window size

wd.maximize_window() #The window is maximized

2. Get the element

wd.find_element_by_link_text('set').click() # Get the link value and perform the click operation

wd.find_element_by_link_text('Search settings').click() # Get the link value and perform the click operation

sel = wd.find_element_by_xpath('//*[@id="nr"]') # Get the drop-down list by xpath

from selenium.webdriver.support.select import Select # Module for drop-down list operations

Select('sel').select_by_value('50') # Change dropdown list settings

3. Multi-list switching

Realize the login of mail.126.com

from selenium import webdriver

import time

wd = webdriver.Chrome()

wd.get('https://mail.126.com/')

# wd.find_element_by_name('email').clear()

# wd.find_element_by_name('email').send_keys('zhanghao') # --- error, element not found

time.sleep(2)

fr = wd.find_element_by_id('x-URS-iframe') # Get the form

wd.switch_to_frame(fr) # switch to this form

time.sleep(3)

wd.find_element_by_name('email').clear() # Clear the value in this element

wd.find_element_by_name('email').send_keys('zhanghao') # Send value to element: zhanghao

 

4. Window closes

wd.close() or wd.quit()

 

5. Support windowless mode

from selenium import webdriver

from selenium.webdriver.chrome.options import Options # webdriver option setting module

import time

chrome_options = Options() # Options initialization

chrome_options.add_argument('--headless') # Windowless mode settings

wd = webdriver.Chrome(chrome_options=chrome_options)

wd.get('http://baidu.com')

title = wd.title

print(title)

time.sleep(2)

wd.close()

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887625&siteId=291194637