程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[UI automation test] selenium + Python 3 Usage Summary (II)

編輯:Python

One 、 Preface

See for installation
【python】Selenium+python3 Use summary ( One )

Two 、 Simple examples of automated scripts

Realization function : Open the browser =》 Enter Baidu website to search =》 Enter in the search box python=》 Click Baidu

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element(by=By.ID, value="kw").send_keys("python")
driver.find_element(by=By.ID, value="su").click()
driver.quit()

3、 ... and 、 Element localization

Element positioning is the core of automated testing

The positioning methods mainly include :(1)id ; (2) name; (3)class name ; (4) tag name ; (5) xpath ; (6) css selector ; (7) link test ; (8) partial link test ;

among xpath There are many positioning applications .

give an example :
<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">

To locate this input Input box , There are several ways :

  • id location

dri.find_element(by=By.ID, value=“kw”).send_keys(“python”)

  • name location

dri.find_element(by=By.NAME, value=“wd”).send_keys(“python”)

  • tag name location

dri.find_element(by=By.TAG_NAME, value=“kw”).send_keys(“python”)

  • class name location

dri.find_element(by=By.CLASS_NAME, value=“s_ipt”).send_keys(“python”)

  • xpath location

dri.find_element(by=By.XPATH, value=“//input[@id=''kw]”).send_keys(“python”)

  • css location

dri.find_element(by=By.CSS_SELECTOR, value=“#kw”).send_keys(“python”)

To locate this text link , There are several ways :

<a href="http://news.baidu.com" target="_blank" class="mnav"> Journalism </a>

  • link text location

dri.find_element(by=By.LINK_TEXT, value=“ Journalism ”).send_keys(“python”)

  • partial link test location

dri.find_element(by=By.PARTIAL_LINK_TEXT, value=“ Journalism ”).send_keys(“python”)

5、 ... and 、 Browser operation

  • Browser maximization

dri.maxmize_window()

  • Set browser width 、 Height

dri.set_window_size(500,500)

  • Browser forward 、 back off

dri.forward()
dri.back()

6、 ... and 、 Operate the test object

The common methods are as follows :

  • click Click on
  • send_keys Input
  • submit Submit
  • clear eliminate
  • text Get the text information of the element

7、 ... and 、 Get attribute value

element.get_attribute(“value”)

8、 ... and 、 Mouse events

ActionChains class :
You need to load
from selenium.webdriver.common.action_chains import ActionChains

  • context_click() Right click

ActionChains(dri).context_click(ppp).perform() #ppp Is the element to be operated

  • double_click() double-click
  • drag_and_dorp() Drag the

Nine 、 Waiting time

1、 Mandatory waiting time time.sleep(3)
2、 Show waiting time WebDriverWait()
3、 Implicit waiting time implicitly_wait()

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import time
driver = webdriver.Chrome()
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\
until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
driver.implicitly_wait(30)
time.sleep(3)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved