说明
本文参考答案基于Chrome,分辨率1920*1080,在其他环境表现可能会不同。 本文代码地址
- 参考书籍下载:
Learning Selenium Testing Tools with Python-2014.pdf
Selenium自动化测试 基于 Python 语言 - 2018.pdf
上机实操: 在新的TAB打开连接
- 打开:https://china-testing.github.io/
- 选择"数据分析"栏目的文章
- 按住"Ctrl+TAB"选择"python"栏目的文章
- 切换到新的标签"python"
- 关闭新的标签"python"
- 关闭浏览器
参考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# qq群144081101 567351477
# CreateDate: 2018-10-17
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://china-testing.github.io/")
driver.implicitly_wait(30)
driver.maximize_window()
element = driver.find_element_by_link_text('数据分析')
element.click()
time.sleep(3)
element = driver.find_element_by_link_text('python')
ActionChains(driver).key_down(Keys.CONTROL).click(element).key_up(
Keys.CONTROL).perform()
time.sleep(3)
driver.switch_to.window(driver.window_handles[1])
time.sleep(3)
driver.close() # 关闭当前TAB
time.sleep(3)
driver.quit()
-
面试问答
-
driver.quit() 和 driver.close()有什么区别?
2.selenium中按下和松开键如何表示?
3.简述ActionChains类的作用?
上机实操: 验证悬浮提示内容
- 打开:http://jqueryui.com/tooltip/
- 鼠标移动到上图的"Your age:"
- 确认悬浮提示内容为'We ask for your age only for statistical purposes.'
-
关闭浏览器
-
参考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# qq群144081101 567351477
# CreateDate: 2018-10-17
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name('demo-frame')
driver.switch_to.frame(frame_elm)
time.sleep(3)
age_field = driver.find_element_by_id('age')
ActionChains(self.driver).move_to_element(age_field).perform()
time.sleep(3)
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located((
By.CLASS_NAME, 'ui-tooltip-content')))
# verify tooltip message
self.assertEqual('We ask for your age only for statistical purposes.',
tool_tip_elm.text)
time.sleep(3)
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main(verbosity=2)
- 面试问答
1.move_to_element()有什么用途?
上机实操: 双击改变颜色
- 打开:http://api.jquery.com/dblclick/
-
双击上图蓝色的框,把颜色该变成黄色
-
参考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# qq群144081101 567351477
# CreateDate: 2018-10-18
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class DoubleClickTest (unittest.TestCase):
URL = 'http://api.jquery.com/dblclick/'
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(self.URL)
self.driver.maximize_window()
def test_double_click(self):
driver = self.driver
frame = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(frame)
box = driver.find_element_by_tag_name('div')
# verify color is Blue
self.assertEqual('rgba(0, 0, 255, 1)',
box.value_of_css_property('background-color'))
ActionChains(driver).move_to_element(
driver.find_element_by_tag_name('body')).perform()
ActionChains(driver).double_click(box).perform()
# verify Color is Yellow
self.assertEqual('rgba(255, 255, 0, 1)',
box.value_of_css_property('background-color'))
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main(verbosity=2)
- 面试问答
1.double_click()有什么用途?
2.rgba的含义?
上机实操: 在新的TAB打开连接
- 打开:http://jqueryui.com/resources/demos/droppable/default.html
- 拖动左边的框到右边
参考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# qq群144081101 567351477
# CreateDate: 2018-10-18
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class DragAndDropTest (unittest.TestCase):
URL = 'http://jqueryui.com/resources/demos/droppable/default.html'
def setUp(self) :
self.driver = webdriver.Chrome()
self.driver.get(self.URL)
self.driver.maximize_window()
def test_drag_and_drop(self):
driver = self.driver
source = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('droppable')
ActionChains(self.driver).drag_and_drop(source, target).perform()
self.assertEqual('Dropped!', target.text)
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main(verbosity=2)
- 面试问答
1.drag_and_drop()有什么用途?
selenium 上机实操: 下拉刷新框中所有内容(实现)
- 打开:http://www.webscrapingfordatascience.com/complexjavascript/
- 抓取框中所有内容。该框按住鼠标中键下滚,会有刷新内容,直至完全加载
参考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# qq群144081101 567351477
# CreateDate: 2018-10-18
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class at_least_n_elements_found(object):
def __init__(self, locator, n):
self.locator = locator
self.n = n
def __call__(self, driver):
elements = driver.find_elements(*self.locator)
if len(elements) >= self.n:
return elements
else:
return False
url = 'http://www.webscrapingfordatascience.com/complexjavascript/'
driver = webdriver.Chrome()
driver.get(url)
# Use an implicit wait for cases where we don't use an explicit one
driver.implicitly_wait(10)
div_element = driver.find_element_by_class_name('infinite-scroll')
quotes_locator = (By.CSS_SELECTOR, ".quote:not(.decode)")
nr_quotes = 0
while True:
# Scroll down to the bottom, now using action (chains)
action_chain = ActionChains(driver)
# Move to our quotes block
action_chain.move_to_element(div_element)
# Click it to give it focus
action_chain.click()
# Press the page down key about 10 ten times
action_chain.send_keys([Keys.PAGE_DOWN for i in range(10)])
# Do these actions
action_chain.perform()
# Try to fetch at least nr_quotes+1 quotes
try:
all_quotes = WebDriverWait(driver, 3).until(
at_least_n_elements_found(quotes_locator, nr_quotes + 1))
except TimeoutException as ex:
# No new quotes found within 3 seconds, assume this is all there is
print("... done!")
break
# Otherwise, update the quote counter
nr_quotes = len(all_quotes)
print("... now seeing", nr_quotes, "quotes")
# all_quotes will contain all the quote elements
print(len(all_quotes), 'quotes found\n')
for quote in all_quotes:
print(quote.text)
input('Press ENTER to close the automated browser')
driver.quit()
- 面试问答
1.如何模拟鼠标中键下滚?
参考资料
- 讨论 qq群144081101 567351477
- 本文最新版本地址
- 本文涉及的python测试开发库 谢谢点赞!
- 本文相关海量书籍下载
- 道家技术-手相手诊看相中医等钉钉群21734177 qq群:391441566 184175668 338228106 看手相、面相、舌相、抽签、体质识别。服务费50元每人次起。请联系钉钉或者微信pythontesting
- 接口自动化性能测试线上培训大纲