Web automated testing (basic learning part 1)

Table of contents

1. Page screenshot operation

1.1 Intercept current page

1.2.1driver.save_screenshot(filename/full_path)

1.2.2driver.get_screenshot_as_file (full path/file name)

1.2.3driver.get_screenshot_as_png()

1.2.4driver.get_screenshot_as_base64()

1.2 Capture the entire page (long screenshot)

2. Method for adding, deleting, modifying and checking page element attributes

3. Pull down the scroll bar method

3.1 Swipe to the bottom of the page

3.2 Swipe to the top

3.3 Swipe to a specific location

3.4 Swipe until the target element is visible

4. Window and Frame switching method

4.1 Introduction to frame

4.2 Window switching

1. Page screenshot operation

1.1 Intercept current page

1.2.1driver.save_screenshot(filename/full_path)

from selenium import webdriver
#  定义一个驱动的对象
driver=webdriver.Chrome()
#  打开需要测试的页面
driver.get("https://www.baidu.com")
#  截屏 
driver.save_screenshot('sanmu.png')

1.2. 2driver.get_screenshot_as_file ( full path/file name)

#  打开需要测试的网页
driver.get('http://news.baidu.com/')
#  截屏
driver.get_screenshot_as_file('sanmu.png')

1.2.3driver.get_screenshot_as_png()

Return the binary data stream, and then save the data by reading and writing the file.

#  打开需要测试的页面
driver.get("https://www.baidu.com")
# 截屏
data=driver.get_screenshot_as_png()
#  通过文件读写形式,把数据存储到本地
with open('sanmu.jpg','wb') as file:
    file.write(data)

1.2.4driver.get_screenshot_as_base64()

Return the data in base64 encoding format, and then store the decoded data in the form of file reading and writing, and complete the screenshot.

#  打开需要测试的页面
driver.get("https://www.baidu.com")
#  通过driver.get_screenshot_as_base64()方法获取屏幕的base64编码格式的数据
base64_data=driver.get_screenshot_as_base64()
#  对数据解码
image_data=base64.b64decode(base64_data)
#  通过文件读写的形式,保存解码后的数据
with open('sanmu.jpg','wb') as file:
    file.write(image_data)

1.2 Capture the entire page (long screenshot)

Execute JavaScript scripts, drag the scroll bar of the page to the bottom of the page, then drag it back to the top, and finally take a screenshot.

from selenium import webdriver
import time

#take_screenshot封装
def take_screenshot(url, save_fn="sanmu.png"):
    driver = webdriver.Chrome() 
    driver.set_window_size(1200, 900)
    driver.get(url) 
    browser.execute_script("""
        (function () {
            var y = 0;
            var step = 100;
            window.scroll(0,0);

         function f() {
            if (y < document.body.scrollHeight) {
                y += step;
                window.scroll(0, y);
                setTimeout(f, 100);
            } else {
                window.scroll(0, 0);
                document.title += "scroll-done";
            }
        }

        setTimeout(f, 1000);
    })();
""")

for i in xrange(30):
    if "scroll-done" in browser.title:
        break
    time.sleep(10)

driver.save_screenshot(save_fn)
driver.close()


if __name__ == "__main__":

take_screenshot("http://baidu.com")

2. Method for adding, deleting, modifying and checking page element attributes

#新增属性
driver.execute_script(“arguments[0].%s=arguments[1]” %attributeName,elementObj, value)
#删除属性
driver.execute_script(“arguments[0].removeAttribute(arguments[1])”,elementObj,attributeName)
#修改属性
driver.execute_script(“arguments[0].setAttribute(arguments[1],arguments[2])”, elementObj, attributeName, value)
#获取属性
elementObj.get_attribute(attributeName)

3. Pull down the scroll bar method

3.1 Swipe to the bottom of the page

js="window.scrollTo(0,document.body.scrollHeight)" 
driver.execute_script(js)

3.2 Swipe to the top

js="window.scrollTo(0,-document.body.scrollHeight)" 
driver.execute_script(js)

3.3 Swipe to a specific location

window.scrollBy(0,-500) # 向上滚动500个像素
window.scrollBy(0,500)   # 向下滑动500个像素
window.scrollBy(-500,0) # 向左滚动500个像素
window.scrollBy(500,0)   # 向右滑动500个像素
driver.execute_script("window.scrollTo(x,y)")  # 滑动到具体位置(坐标)

3.4 Swipe until the target element is visible

driver.execute_script("arguments[0].scrollIntoView(true);", element)  # 向下滚动至元素可见
driver.execute_script("arguments[0].scrollIntoView(false);",element)  # 向上滚动至元素可见

4. Window and Frame switching method

4.1 Introduction to frame

     The frame element is in the html syntax, and it will contain another embedded html document. When using selenium to open a web page, the operation scope defaults to the current html, and does not include the content in the embedded html document.

     If you want to operate the elements in the embedded html document, you must switch the scope of operation to the embedded document.

driver.switch_to.frame(frame_reference)#切换
driver.switch_to.default_content()#切回原html页面

4.2 Window switching

     When operating on a web page, clicking a link or button will open a new window, and it is necessary to switch the window to locate the current window element.

#简单切换
handles = driver.window_handles
driver.switch_to.window(handles[1])#开始访问的是句柄0,第二个页面是1
#循环遍历确定
for handle in driver.window_handles:
    # 先切换到该窗口
    wb.switch_to.window(handle)
    # 得到该窗口的标题栏字符串,判断是不是要操作的窗口
    if '窗口标题' in driver.title:
        # 如果是,那么WebDriver对象就是对应的该窗口,跳出循环
        break

If you want to jump back to the original page: 

#变量保存当前窗口的句柄
mainWindow = driver.current_window_handle
#切换到初始窗口
driv.switch_to.window(mainWindow)

Guess you like

Origin blog.csdn.net/m0_58807719/article/details/130036389