3

Selenium+Python系列(三) - 常见浏览器操作 - 久曲健

 1 year ago
source link: https://www.cnblogs.com/longronglang/p/16815021.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

上篇文章为大家分享了自动化测试中,常见元素定位的操作。

今天再次读文章,居然忘记了大家特别喜欢的CSSXpath定位操作分享,这怎么能行呢?

马上安利,感兴趣的同学去参考下面链接👇:

浏览器常见操作

1、打开某个页面

python
driver.get("http//:www.baidu.com")

2、获取当前URL

python
currentUrl=driver.current_url
print('当前地址:{0}'.format(currentUrl))

3、获取页面源代码

python
pagesourse=driver.page_source
print('获取页面源代码:{0}'.format(pagesourse))

4、获取当前标题

python
title=driver.title
print('当前页面标题:{0}'.format(title))

5、前进/后退操作

python
driver.get("https://www.cnblogs.com/longronglang/p/16811786.html");
print('当前页面标题:{0}'.format(title))
python
driver.forward()
python
driver.back()

8、刷新浏览器

python
driver.refresh()

9、获取使用浏览器

python
print("当前使用浏览器:{0}".format(driver.name))

10、退出浏览器

  • 关闭当前窗口,或最后打开的窗口:
    driver.close()
  • 关闭全部关联窗口,而且安全关闭session:
    driver.quit()

11、最大化浏览器窗口

driver.maximize_window()

12、设置浏览器窗口大小

driver.set_window_size(1980,1080)

13、获取当前窗口的长和宽

driver.get_window_size()

14、获取当前窗口坐标

driver.get_window_position()

关于driver的常用操作,笔者都已作出了演示,其他可参考源代码去学习,或者去通过输出driver属性,识别函数去学习即可。

15、如何输出driver属性

什么是属性?属性就是属于一个对象的数据或者函数的元素(内建函数dir可查看对象属性),能够经过属性据点标识符来访问。

示例代码如下:

python
# 输出driver属性
print('浏览器属性:')
print(dir(driver))

全部案例代码如下:

highlighter- Bash
# -*- coding: utf-8 -*-
"""
@Time : 2022/10/21 8:31 PM
@Auth : 公众号:软件测试君
@File :brower.py
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import time as time

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.baidu.com/")
# 设置指定大小窗口
driver.set_window_size(1980, 1080)
# 最大化
driver.maximize_window()
# 获取当前URL
currentUrl = driver.current_url
print('当前地址:{0}'.format(currentUrl))
# 获取页面源代码
pagesourse = driver.page_source
print('获取页面源代码:{0}'.format(pagesourse))
# 获取当前标题
title = driver.title
print('当前页面标题:{0}'.format(title))
# 前进/后退操作
driver.get("https://www.cnblogs.com/longronglang/p/16811786.html");
print('当前页面标题:{0}'.format(title))
time.sleep(2)
# 前进
driver.forward()
time.sleep(2)
# 后退
driver.back()
# 刷新浏览器
driver.refresh()
# 获取使用浏览器
print("当前使用浏览器:{0}".format(driver.name))
# 输出driver属性
print('浏览器属性:')
print(dir(driver))
# 退出浏览器
# 关掉句柄
driver.close()
# 退出浏览器
driver.quit()

这部分相对来说比较简单,感兴趣的同学可以自行去尝试!

我是六哥,如果觉得写的还不错,请继续关注我,我的公众号:软件测试君,并帮忙转发文章到朋友圈,你的每一次转发,我都当做了喜欢!🙏

__EOF__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK