Selenium Use finishing

I used Python to use selenium library, but you can also use java, python but not as easy to operate. Will hereinafter python to operate as an example, selenium organize my learning process collected.

 

A: install

First python first install selenium:

pip install Selenium 

The second step, download the corresponding drive browser, I use chrome (recommended), corresponding driver download address: http://chromedriver.storage.googleapis.com/index.html

Drive version to correspond with the chrome version, but it is recommended the direct use of the latest version of chrome, and then download the latest driver also present on it. Under the path and then download the file into chromedriver.exe environment variables, make sure to call python correctly.

 

Two: Basic usage

Import:

from selenium import webdriver

 

1. Obtain the driver objects

driver = webdriver.Chrome()

If you want to set the parameters , create an option and the object as a parameter in the constructor:

= chromeOptions webdriver.ChromeOptions () 
chromeOptions.add_argument ( ' disable-infobars ' ) # cancel the display when the browser is opened monitor prompt 
chromeOptions.add_argument ( ' --user-Agent = Mozilla / 5.0 (Windows NT 6.1; WOW64; rv : 34.0) Gecko / Firefox 20,100,101 / 34.0 ' ) # add-agent the User 
chromeOptions.add_argument ( " --proxy-Server = HTTP: //127.0.0.1 " ) # add the proxy chromeOptions .add_argument ( ' --headless' ) # browser running in the background mode 
driver = webdriver.Chrome (options = chromeOptions)

 

2. Basic Operations

driver.maximize_window () # browser maximized (the default is a small window) 
driver.minimize_window () # minimize the browser 
driver.set_page_load_timeout (20) # timeout, if set, over time will automatically stop 
driver.get ( " http://www.baidu.com " ) # access the site 
driver.refresh () # refreshed website 
JS = ' window.open (" https://www.baidu.com "); '  # by executing js, open a the new window 
driver.execute_script (JS) 
driver.switch_to.window (driver.window_handles [ . 1]) # switching the browser window, window_handles set the current window, where the representative switch to the second window, if there is a second a window will complain 
driver.getTitle () # get the current window title name
driver.current_window_handle # get the current window object
driver.current_url # current url
driver.page_source # source code for the current page
driver.close () # close the current window driver.quit () # close the entire browser

 

III. Finding Elements

Basics Find

driver.get ( " https://www.baidu.com " ) 
kw = find_element_by_id ( " kw " ) # Find html element by id, corresponding to the Baidu search box 
su = driver.find_element_by_id ( " su " ) # click on the search button 
kw .send_keys ( " search " ) # elements can use this method to automatically populate the input box contents 
su.click () # elements can use this method click on itself 

# other lookup methods 
find_element_by_name () # Find html element by the name attribute 
find_element_by_class_name () # Find html element through the class attribute values that exist in 
find_element_by_tag_name () #Find by looking for the label elements, such as incoming "p", "input", which does not recommend the use of 

# there are many other methods, above all return a single element, element plus s return was more qualified elements 
find_elements_by_name ()

 

Advanced Find

A xpath find :

xpath is an XML Path Language, which can be used to determine the position of the element xml document, to complete the look of elements by path elements. HTML is an implementation of XML, so xpath is a very powerful way of positioning.

Absolute path 1.1 Xpath location :

Html tag name using the hierarchical relationship of the elements to locate the absolute path, the subscript numbers in a plurality of identical labels may be selected in order to find the general <html> tag from the start down. Such as:

find_element_by_xpath("/html/body/div[1]/form/span[1]/input")

1.2 Xpath element attribute value using the positioning

find_element_by_xpath("//input[@id='kw']")
find_element_by_xpath("//*[@name='wd']")

1.3 hybrid positioning

find_element_by_xpath("//form[@id='form']/span/input")

 

Two. CSS selectors

driver.find_element_by_css_selector(".btn")
driver.find_element_by_css_selector("#su")

By property or in combination:

driver.find_element_by_css_selector("input[name='kw']").send_keys("Python")
driver.find_element_by_css_selector("span.bg.btn_default>input#su").click()

 

IV. Mouse and keyboard

selenium provides ActionChains classes to handle the mouse, keyboard events, such as mouse movement, click, drag, lift the keyboard is pressed and so on. You need to import the following

from selenium.webdriver.common.action_chains import ActionChains
ActionChains list of methods:
click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素
 
  • 链式写法:
su = find_element_by_id("su")
ActionChains(driver).move_to_element(su).click(su).perform()
 
  • 分步写法
su = find_element_by_id("su")
actions = ActionChains(driver)
actions.move_to_element(su)
actions.click(su)
actions.perform()

控制键盘示例:

ActionChains(driver).key_down(Keys.CONTROL, enter).key_up(Keys.CONTROL).perform() #按下Ctrl键然后松开

 执行JS:

driver.execute_script("window.scrollTo(0,100)")
driver.execute_script("document.getElementById(\"kw\").value=\"selenium\"")

 

五. 其他

禁用图片加载:

from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {
    'profile.default_content_setting_values': {
         'images': 2,
         'javascript': 2,
    }
} #2是禁用, 1是允许
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)    

 

隐藏window.navigator.webdriver特征参数 (有时候能反爬)

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = Chrome(options=option)

 

Guess you like

Origin www.cnblogs.com/soledadcat/p/12002864.html