Basic usage of selenium

Getting Started

Calling environment variables specified PhantomJS browser create browser object

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")

get method will wait until the page is fully loaded, and then will continue the program, testing will usually be selected here time.sleep (2)

driver.get('https://www.baidu.com/')

Get the page called wrapper of id tag text content

data = driver.find_element_by_id('wrapper').text
data
'新闻\nhao123\n地图\n视频\n贴吧\n学术\n登录\n设置\n更多产品\n下载百度APP\n有事搜一搜  没事看一看\n把百度设为主页关于百度About  Baidu百度推广\n©2019 Baidu 使用百度前必读 意见反馈 京ICP证030173号  京公网安备11000002000001号 '

Print page titled "Baidu, you know."

driver.title
'百度一下,你就知道'

Generates a snapshot of the current page and save

driver.save_screenshot('baid.png')
True
driver.save_screenshot('baid.png')
True

id = "kw" is Baidu search input box character string "Ma"

driver.find_element_by_id('kw').send_keys('马云')

id = "su" is Baidu search button, click () is simulate a click

driver.find_element_by_id('su').click()

Get a new snapshot of the page

driver.save_screenshot('马云.png')
True

After the print page rendering source code

driver.page_source
'<html><head>\n    \n    <meta http-equiv="content-type" content="text/html;charset=utf-8"><style data-for="result" id="css_result" type="text/css">body{color:#333;background:#fff;padding:6px 0 0;margin:0;position:relative;min-width:900px}body,th,td,.p1,.p2{font-family:arial}p,form,ol,ul,li,dl,dt,dd,h3{margin:0;padding:0;list-style:none}input{padding-top:0;padding-bottom:0;-moz-box-sizing:border-box;-webkit-box-sizing:b
.....

Get the current page Cookie

driver.get_cookies()
[{'domain': 'www.baidu.com',
  'httpOnly': False,
  ....

To call the keyboard operation is necessary to introduce keys package

from selenium.webdriver.common.keys import Keys

ctrl + a select all content input box

driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')

ctrl + x content input box shear

driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')

Re-enter the contents of the input box

driver.find_element_by_id('kw').send_keys('王健林')
driver.find_element_by_id('su').send_keys(Keys.RETURN)

Clear input box contents

driver.find_element_by_id('kw').clear()

Generate a new page snapshot

driver.save_screenshot('王健林.png')
True

Gets the current url

driver.current_url
'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E7%8E%8B%E5%81%A5%E6%9E%97&oq=%25E9%25A9%25AC%25E4%25BA%2591&rsv_pq=91f8d64b0000e74c&rsv_t=baa2FFFF1WZYWQhU5LpuRXBg92yxLF4VBoGGv3MDbXE3Rs7uYv9kYjjnNxQ&rqlang=cn&rsv_enter=0&rsv_dl=tb&inputT=49232&rsv_sug3=5&bs=%E9%A9%AC%E4%BA%91'

Close the current page, if there is only one page, the browser closes

driver.close()

Close the browser

driver.quit()

Page Actions

Selenium is WebDriver provides various ways to find elements assume a form inputs the following:

We have to operate above

<input type="text" name="user-name" id="passwd-id" />

Gets id tag value

element = driver.find_element_by_id('pwsswd_id')

Gets name tag values

element = driver.find_element_by_name('username')

Gets the signature value

element = driver.find_elements_by_tag_name('input')

By matching XPath

element = driver.find_element_by_xpath('//input[@id='passwd-id']')

Custom UI elements

For the selection element, the following single element selected API

find_element_by_id
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

By ID

<div id="coolestWidgetEvah">...</div>
...
element = driver.find_element_by_id("coolestWidgetEvah")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")

By Class Name

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
Cheddar
Gouda
cheeses = driver.find_elements_by_class_name("cheese")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")

By Tag Name

<iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")

By Name

<input name="cheese" type="text"/>

cheese = driver.find_element_by_name("cheese")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")
<a href="http://www.google.com/search?q=cheese">cheese</a>

cheese

cheese = driver.find_element_by_link_text("cheese")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.LINK_TEXT, "cheese")
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>

search for cheese>

cheese = driver.find_element_by_partial_link_text("cheese")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")

By CSS

<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
milk cheese
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")

By XPath

<input type="text" name="example" />
<INPUT type="text" name="other" />


inputs = driver.find_elements_by_xpath("//input")
------------------------ or -------------------------
from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")

Mouse movements chain

1566731524447

Sometimes, we need to simulate some of the mouse on the page, such as double-click, right-click and drag and even hold it there, and we can import ActionChains like to do:

Class introduced ActionChains

from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com/')

Mouse to move to the position ac

ac = driver.find_element_by_xpath('element')
ActionChains(driver).move_to_element(ac).perform()

Click on

ac = driver.find_element_by_xpath("elementA")
ActionChains(driver).move_to_element(ac).click(ac).perform()

Double-click

ac = driver.find_element_by_xpath("elementB")
ActionChains(driver).move_to_element(ac).double_click(ac).perform()

Right-click

ac = driver.find_element_by_xpath("elementC")
ActionChains(driver).move_to_element(ac).context_click(ac).perform()

Stopped

ac = driver.find_element_by_xpath('elementF')
ActionChains(driver).move_to_element(ac).click_and_hold(ac).perform()

Ac1 drag to position the ac2

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

Fill out forms

<select id="status" class="form-control valid" onchange="" name="status">
    <option value=""></option>
    <option value="0">未审核</option>
    <option value="1">初审通过</option>
    <option value="2">复审通过</option>
    <option value="3">审核不通过</option>
</select>

# 导入 Select 类
from selenium.webdriver.support.ui import Select

# 找到 name 的选项卡
select = Select(driver.find_element_by_name('status'))

# 
select.select_by_index(1)
select.select_by_value("0")
select.select_by_visible_text(u"未审核")

1566731500008

These are three choices in drop-down box, which can be selected according to the index, may be selected according to the value, you may be selected based on the character. note:

--index index starting from 0
is a property value option tag --value, not the value displayed in the drop down box
--visible_text option value in the label text, is a value in the drop box

cancel all of them

select.deselect_all()

Pop processing

alert = driver.switch_to_alert()

Page switching

driver.switch_to.window("this is window name")

or

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

Cookies

# 获取页面每个Cookies值,用法如下
for cookie in driver.get_cookies():
    print "%s -> %s" % (cookie['name'], cookie['value'])
# 删除Cookies,用法如下
# By name
driver.delete_cookie("CookieName")

# all
driver.delete_all_cookies()

Page wait

1566731484247

Note: This is a very important part! !

Now more and more web pages using Ajax technology, so that the program can not determine when an element is fully loaded out. If the actual page you wait too long resulting in a dom element has not come out, but your code directly using this WebElement, the exception will be thrown NullPointer.

To avoid this difficulty and positioning elements will increase the probability of occurrence of ElementNotVisibleException. Therefore Selenium provides two ways to wait, wait one is implicit, explicit one is waiting.

Implicit wait is waiting for a specific period of time, explicitly specify a certain condition is wait until the conditions are met to continue.

Explicit waiting

Explicitly specify a wait condition, and then set the maximum wait time. If you have not found elements at this time, it will be thrown up.

from selenium import webdriver
from selenium.webdriver.common.by import By
# WebDriverWait 库,负责循环等待
from selenium.webdriver.support.ui import WebDriverWait
# expected_conditions 类,负责条件出发
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("http://www.xxxxx.com/loading")
try:
    # 页面一直循环,直到 id="myDynamicElement" 出现
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

1566731470363

If you do not write parameters, the program will default 0.5s called once to see whether the element has been generated, if the element is that there have been, then returns immediately.

Here are some built-in waiting conditions, these conditions you can call directly, without having to write a certain wait condition.

title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable – it is Displayed and Enabled.
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present

Implicit wait

Implicit wait relatively simple, is to simply set a waiting time in seconds.

from selenium import webdriver

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

Guess you like

Origin www.cnblogs.com/marklijian/p/11408932.html