> 技术文档 > Python第三方库Selenium基础操作_python下载selenium

Python第三方库Selenium基础操作_python下载selenium


安装Selenum

1、直接在pycharm软件里面下载第三方库,右下角interpreter Setting

2、点击左上角的+号

3、搜索selenium,勾选option,填写:-i https://pypi.tuna.tsinghua.edu.cn/simple,再点击install pakage

下载chorm浏览器

网址:Google Chrome 网络浏览器

下载chorm浏览器驱动

下载网址:Chrome for Testing availability

注意浏览器驱动的版本要和chorm的版本相近,起码大版本要相近

如浏览器版本:

浏览器驱动版本:我这里就选对应上面浏览器,对应130的大版本一致

 

选择对应的版本就行,我这里选的是windos的:

 关闭浏览器自动更新功能,避免后期浏览器版本升级,驱动不能使用:

步骤1:电脑搜索  服务  设置

步骤2:把谷歌对应的内容,全部禁用 

将下载好的驱动放在pycharm文件目录下:

 

一、浏览器相关操作

1、配置浏览器、打开浏览器

这里的浏览器驱动,最好写绝对路径,避免出现识别不了的情况

# 用于操作浏览器from selenium import webdriver# 用于设置谷歌浏览器from selenium.webdriver.chrome.options import Options# 用于管理浏览器驱动from selenium.webdriver.chrome.service import Service# 创建浏览器对象q1=Options()# 禁用沙盒模式, 解决Linux下权限问题,兼容性问题q1.add_argument(\"--no-sandbox\")# 保持浏览器打开状态(默认是代码执行完毕后自动关闭)q1.add_experimental_option(\"detach\",True)# 创建浏览器对象,通过驱动启动浏览器a1=webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"),options=q1)

2、打开网页

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceimport time# 设置启动浏览器def setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1# 打开浏览器a1=setup()# 打开网页(百度)a1.get(\"https://baidu.com/\")time.sleep(5)# 关闭当前标签页a1.close()# 退出浏览器并释放驱动a1.quit()

 3、浏览器窗口最大化、最小化

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")time.sleep(5)# 窗口最大化a1.maximize_window()time.sleep(5)# 窗口最小化a1.minimize_window()time.sleep(2)a1.maximize_window()time.sleep(5)a1.close()a1.quit()

 4、设置浏览器打开的位置和尺寸

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 设置浏览器位置和尺寸a1.set_window_position(100,100)a1.set_window_size(500,500)a1.close()a1.quit()

5、浏览器截图、刷新网页

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 浏览器页面截图a1.get_screenshot_as_file(\"D:\\\\images\\\\1.png\")time.sleep(2)# 刷新网页a1.refresh()

二、 元素相关操作

1、简介元素定位

可以通过浏览器的控制台docume

nt.get....操作来查看该元素定位是否有多个

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Service# 导入元素定位的包from selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 查找一个元素(找到的话返回结果,找不到报错)a2=a1.find_element(By.ID,\"kw\")# 查找多个元素,找到返回列表,找不到报错a3=a1.find_elements(By.ID,\"kw\")# 浏览器控制台查找元素:document.getElementById(\"kw\")print(a3)# 注意:# 1、通过id定位元素一般比较准确# 2、并不是所有网页或者元素都有id值

2、简介元素交互操作

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 定位搜索框的元素a2=a1.find_element(By.ID,\"kw\")# 元素输入a2.send_keys(\"python\")time.sleep(4)# 定位搜索按钮的元素a3=a1.find_element(By.ID,\"su\")# 点击搜索按钮a3.click()time.sleep(4)# 清空搜索框a2.clear()

3、八大元素定位

最常用的是By.XPATH方式

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 定位搜索框的元素# a1.find_element(By.ID,\"kw\").send_keys(\"selenium\")# a1.find_element(By.NAME,\"wd\").send_keys(\"python\")# a1.find_element(By.CLASS_NAME,\"s_ipt\").send_keys(\"python\")# 1、class值不能有空格,否则报错:bg s_btn# a1.find_element(By.CLASS_NAME,\"bg s_btn\").click()# 2、class值如果有多个,可以根据列表的索引值定位# a1.find_elements(By.CLASS_NAME,\"icon-bg--icon\")[1].click()# 3、class值如果有多个,你返回的不是列表,则会将查找到的第一个元素作为value值定位# a1.find_element(By.CLASS_NAME,\"icon-bg--icon\").click()# 4、有些class值的网站是随机的,无法用class元素定位#根据尖括号里的开头标签定位# a1.find_elements(By.TAG_NAME,\"a\")[2].click()# 只找a标签里的精准文本为“番剧”的元素,值如果有多个,可以根据列表的索引值定位# a1.find_element(By.LINK_TEXT,\"番剧\").click()# 只找a标签里的模糊文本含有“番”的元素# a1.find_element(By.PARTIAL_LINK_TEXT,\"番\").click()# 元素定位-CSS_SELECTOR# 1、.class = 点+class值 通过class定位# 2、#id = #+id值 通过id定位# 3、tagName = 标签名称 通过标签名称定位# 4、通过任意类型定位:\"[类型=\'精准值\']\"# 5、通过任意类型定位:\"[类型*=\'模糊值\']\"# 6、通过任意类型定位:\"[类型$=\'结尾值\']\"# 7、通过任意类型定位:\"[类型^=\'开头值\']\"# 8、更简单的定位方式:在谷歌控制台直接复制 SELECTOR,缺点:value值可能很长# a1.find_element(By.CSS_SELECTOR,\"#kw\").send_keys(\"python\")# 元素定位-XPATH# 1、复制浏览器Xpath(通过属性+路径定位,属性如果是随机值可能定位不准确)# 2、复制浏览器Xpath 完整路径(缺点是定位值长,优点100%准确定位)a1.find_element(By.XPATH,\"/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input\").send_keys(\"python\")

在浏览器,选中你要查看的元素右键,点击检查,对应元素会深色表示,再右键深色部分,可以复制selector元素的value值 

在浏览器,选中你要查看的元素右键,点击检查,对应元素会深色表示,再右键深色部分,可以复制full XPath元素的value值

三、网页相关操作

1、隐形等待时间

允许在规定时间内先把所有元素都加载出来,再定位相关元素进行操作

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://baidu.com/\")# 元素定位隐形等待(多少秒内找到元素就立刻执行,没找到元素就报错)# 这是一个通用操作,后续操作元素都会默认30秒内找到元素就立刻执行,无需重复添加这个操作a1.implicitly_wait(30)a1.find_element(By.ID,\"kw\").send_keys(\"selenium\")

 2、上传文件

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://the-internet.herokuapp.com/upload\")a1.implicitly_wait(30)# 上传文件用.send_keysa1.find_element(By.XPATH,\"/html/body/div[2]/div/div[1]/form/input[1]\").send_keys(r\"D:\\images\\1.png\")

 3、获取句柄、切换标签页操作

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://www.baidu.com/\")a1.implicitly_wait(30)for i in range(3): # 获取当前浏览器所有标签页句柄 a2=a1.window_handles # 关闭a1标签页 a1.close() # 将a1标签页切换到a2[1]标签页 a1.switch_to.window(a2[1]) time.sleep(3) # 查看当前操作的标签页 print(a1.current_window_handle)

 4、利用多线程,开多个浏览器进行操作

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport threadingimport timeclass A1: def __init__(self,x,y): self.x=x self.y=y def setup(self): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) a1.set_window_position(self.x,self.y) a1.implicitly_wait(30) a1.get(\"https://www.baidu.com/\") return a1 def execute(self): a1 = self.setup()for i in range(4): i=A1(100*i,100*i) threading.Thread(target=i.execute).start()

 5、点击弹窗的确认和取消按钮

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://the-internet.herokuapp.com/javascript_alerts\")a1.implicitly_wait(30)a1.find_element(By.XPATH,\"/html/body/div[2]/div/div/ul/li[1]/button\").click()time.sleep(3)# 获取弹窗文本print(a1.switch_to.alert.text)# 弹窗输入文本a1.switch_to.alert.send_keys(\"123啦啦啦啦\")# 点击弹窗确定按钮a1.switch_to.alert.accept()# 点击弹窗取消按钮a1.switch_to.alert.dismiss()

6、iframe嵌套页面

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://the-internet.herokuapp.com/iframe\")a1.implicitly_wait(30)a2=a1.find_element(By.XPATH,\"/html/body/div[2]/div/div/div/div[1]/div[2]/div[1]/iframe\")# 进入iframe嵌套页面a1.switch_to.frame(a2)# 进入iframe嵌套页面,点击文本框a1.find_element(By.XPATH,\"/html/body/假设进入了\").click()# 退出嵌套页面,切换回默认页面a1.switch_to.default_content()

 7、获取文本内容 text,是否可见

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://the-internet.herokuapp.com/javascript_alerts\")a1.implicitly_wait(30)# 获取文本内容 textprint(a1.find_element(By.XPATH,\"/html/body/div[2]/div/div/ul/li[1]/button\").text)# 判断文本内容是否可见,可见返回True,不可见返回Falseprint(a1.find_element(By.XPATH,\"/html/body/div[2]/div/div/ul/li[1]/button\").is_displayed())

8、网页前进、后退

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timedef setup(): q1 = Options() q1.add_argument(\"--no-sandbox\") q1.add_experimental_option(\"detach\", True) a1 = webdriver.Chrome(service=Service(r\"D:\\it_python\\Selenium\\chromedriver.exe\"), options=q1) return a1a1=setup()a1.get(\"https://www.baidu.com/\")a1.implicitly_wait(30)a1.find_element(By.NAME,\"wd\").send_keys(\"python\")time.sleep(2)a1.find_element(By.ID,\"su\").click()time.sleep(2)# 网页后退a1.back()time.sleep(5)# 网页前进a1.forward()