首页 > 其他分享 >selenium使用方法

selenium使用方法

时间:2022-08-17 15:24:54浏览次数:61  
标签:keys selenium driver send element 使用 方法 find

'''
## **认识selenium**

​ **下载:pip install selenium**

​ 官方文档:https://selenium-python.readthedocs.io/

### 什么是selenium?

​ selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Grid)。Selenium的核心Selenium Core基于JsUnit,完全由JavaScript编写,因此可以用于任何支持JavaScript的浏览器上。

**selenium可以模拟真实浏览器,自动化测试工具,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。**
'''
### 元素定位


from selenium import webdriver
# 指定浏览器
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# 打开指定窗口
driver.get('https://www.baidu.com/')

# 打开第二个窗口
# driver.execute_script("window.open(https://taobao.com)")
# 切换窗口
# driver.switch_to.window(driver.window_handles[1])

# 获取网页源代码
# print(driver.page_source)
# **find_element_by_id 通过id属性查找**

# 定位网页元素 send_keys输入
# driver.find_element(By.ID,"kw").send_keys('大黑牛')

# **find_element_by_class_name 通过类名查找**
# 通过类名去获取
# driver.find_element(By.CLASS_NAME,"s_ipt").send_keys('小少爷')

# **find_element_by_name 通过name属性查找**
# 通过name去获取
# driver.find_element(By.NAME,"wd").send_keys('蓝蓝')

# **find_element_by_tag_name 通过标签名查找** ---“用的少”
# 通过标签获取 一个网页当中肯定有多个相同的标签,不好定位,不推荐使用
# driver.find_element(By.TAG_NAME,"div")


# **find_element_by_xpath 通过xpath语法来获取**
# 通过xpath定位
# driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('牛牛')


# **find_element_by_css_selector 通过css查找**
# 通过css语法定位
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys('Python')

# **find_element_by_partial_link_text 通过部分文本文档查找**
# 通过关键字定位
# driver.find_element(By.PARTIAL_LINK_TEXT,"神十三").click()

# 程序运行自动关闭窗口
# driver.close

标签:keys,selenium,driver,send,element,使用,方法,find
From: https://www.cnblogs.com/longwanghzx/p/16595353.html

相关文章

  • 一天一个知识点-----require.context的简单使用
    require.contextrequire.context是webpack提供的apirequire.context(directory,useSubdirectories,regExp)directory:表示检索的目录useSubdirectories:表示是否检索子......
  • spring boot 定时任务使用
    1.在Springboot启动类上添加注解:@EnableScheduling2.在需要执行定时任务的类上加@Component注解,在需要执行的方法上加@Scheduled(cron="0/2*****")注解@Compon......
  • [Python学习笔记]Python基础-12 面向对象编程_属性和方法
    内置属性可以使用内置函数dir查看一个对象支持的所有属性和方法,Python中存在很多的内置属性classPerson(object):def__init__(self,name,age)->None:......
  • 使用自定义字体 vue electron
    1,把自定义字体包导入项目  2,font.css文件下1字体名2字体路径3,4字体样式(可省略) 3,App。vue文件中修改样式中的font-family属性   ......
  • Path.GetDirectoryName方法
    命名空间:System.IO程序集:System.Runtime.dllPath.GetDirectoryName()返回指定路径的目录信息参数pathString文件或目录的路径。返回Stringpath 的目录信息;如果 ......
  • switch 与if else 的区别和使用差别
    switch关键特性是 1key ===value 严格判断是否switch(key){casevalue:......
  • string.IsNullOrEmpty()方法以及C#中的示例
    String.IsNullOrEmpty()方法是String类的内置方法,用于检查字符串是Null还是Empty ?如果未使用正确的值初始化字符串对象,则将其视为“空字符串” ;如果已初始化字符串对象......
  • 使用cpolar发布树莓派网页(cpolar隧道的完善)
    在上篇文章中,我们为树莓派上的网页设置了能够长期稳定存在的数据隧道,但此时的网页和数据隧道还存在一些小问题,如关闭cpolar的链接窗口后,在外网会无法访问树莓派网页;树莓派......
  • 使用cpolar发布树莓派网页(apache2网页的发布)
    在上篇介绍中,我们成功的在本地树莓派上建立起一个简单网页,不过在通常情况下,树莓派并不会随身携带,而是将其放在固定的地方(如家里),想要在其他地方访问到树莓派上的网页,就需要......
  • leetcode91-解码方法
    解码方法dp如果当前位置的字符串不等于0,则表明这个字符可以被解码成新字符,dp[i]+=dp[i-1]如果上一个位置的字符串等于1或者2并且与当前字符拼接的数字小于等于26,则......