selenium基础知识-python

发布于:2024-12-18 ⋅ 阅读:(83) ⋅ 点赞:(0)

一、下载selenium

pip install selenium==4.1.1   --ignore-installed

二、导包

from selenium import webdriver
#用于支持下拉列表
from selenium.webdriver.support.select import Select
from time import sleep

三、浏览器

用于打开浏览器操作,首先需要把浏览器驱动放置到python目录中。

#谷歌
driver=webdriver.Chrome()
#ie
driver=webdriver.Ie()
#火狐
driver=webdriver.Firefox()

四、打开和关闭浏览器

4.1打开浏览器

通过网址打开

driver.get("url");

4.2 关闭浏览器

close是仅关闭浏览器,quit是关闭浏览器同时关闭驱动

driver.quit();
driver.close();

五、等待

等待分为隐式等待和显式等待

#显式等待
sleep(3);
#隐式等待
driver.implicitly_wait(30)

六、定位页面元素

6.1 单一元素

可以采用name、id、xpath、link_text(链接文字)、class_name、tag_name

driver.find_element_by_class_name();
driver.find_element_by_css_selector();
driver.find_element_by_xpath();
driver.find_element_by_id();
driver.find_element_by_name();
driver.find_element_by_link_text()

6.2 多元素

采用elements

driver.find_elements_by_name()

6.3 select

用于识别下拉列表元素,需要导入包from selenium.webdriver.support.select import Select

才能使用

#找到下拉列表
driver.Select(driver.find_element_by_id())
Select(driver.find_element_by_id()).select_by_value("值");
Select(driver.find_element_by_id()).select_by_index("下拉框选项序号")
#取消相应的文本选项
Select(driver.find_element_by_id()).select_by_visible_text();
#取消所有选项
Select(driver.find_element_by_id()).deselect_all()
#取消所有选项
Select(driver.find_element_by_id()).deselect_all()
#取消对应的文本选项
Select(driver.find_element_by_id()).deselect_by_index()
#返回第一个选项
Select(driver.find_element_by_id()).first_selected_option()
#返回所有选项
Select(driver.find_element_by_id()).all_selected_options();

七、获取页面元素属性

获取网页标题可使用 title

获取页面元素的文本值text

获取指定属性的值attribute

send_keys可用于键盘输入数据

click用于点击页面元素

#获取页面标题
driver.title();
#获取元素文本
driver.find_element_by_id("kw").text();
#输入
driver.find_element_by_id("kw").send_keys("输入内容");
#点击
driver.find_element_by_id("su").click();

八、切换框架/窗口

切换框架和窗口需使用switch_to

#切换alert弹窗
driver.switch_to.alert();
#定位到当前聚集的元素上
driver.switch_to.active_element();
#切换到主页面
driver.switch_to.default_content();
#切换到某个frame
driver.switch_to.frame("编号或者name")
#切换到上一层frame
driver.switch_to.parent_frame()
#切换到指定的window_name
driver.switch_to.window()


九、增强脚本

9.1 检查点

 in运算符,用于判断字符是否存在于另外一个字符中

 if语句,判断条件是否成立

if 条件 :
    执行语句;
"abc" in "abcd"

9.2 读取txt文件

读取txt文件采取file=open(文件名,'r')返回文件对象,文件默认编码utf-8,r表示读取文件

遍历文件需采用for循环

如果需要对文件数据进行提取可采用split()分割函数

#打开文件
file=open("文件名","r");
for x in file :
    print(x)
    #把文件中所有行读取到一个列表中,换行符可以读出
    list=file.readline();
#关闭文件
file.close()

9.3 读取EXCEL

需要安装pandas才可以使用读取EXcel

当无列名c

import pandas
#读取数据
data=pandas.read_excel("文件位置",names=['别名'],dtype={'列号':数据类型})
print(data)

9.4 访问数据库

需要安装pymysql,这是用于支持python连接mysql

连接数据库conn=pymysql.connect(host='服务器 IP', port=3306, user='用户名', passwd='密码',
db='数据库名', charset='utf8')

import pymysql
#连接数据库
conn=pymysql.connect(host="localhost",port=3306,user="数据库用户名",password="密码",db="数据库名称",charset="utf8");
#用游标读取数据
rs=conn.cursor();
#写sql语句
sql="";
rs.execute(sql);
#获取数据,是以元组的形式
data=rs.fetchall();
#关闭游标
rs.close()
#关闭数据库
conn.close()
print(data)

数据库添加数据

import pymysql
#连接数据库
conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",db="jhh",charset="utf8");
#游标
rs=conn.cursor();
#sql语句
sql="insert 语句";
rs.execute(sql);
#提交数据库,不可省略
conn.commit();
rs.close();
conn.close();

批量添加数据,可以使用一次性批量提交数据

import pymysql
#连接数据库
conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",db="jhh",charset="utf8");
data=[];
for i  in range(1,10):
#将元组存入列表
    data.append();
sql="insert 语句"
#存入批量数据
rs.executemany(sql,data);
conn.commit();
rs.close();
conn.close();

网站公告

今日签到

点亮在社区的每一天
去签到