selenium基本操作总结(一)

本文总结了Selenium的基本操作,包括元素定位(如通过ID、Name、Link Text等)、鼠标事件(如右键、双击、悬停)、键盘事件(如RETURN键操作)以及Cookie的保存和读取。在执行鼠标和键盘事件时,需使用ActionChains并调用.perform()方法。同时,介绍了如何进行页面截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

selenium基本操作总结

1.selenium基本定位
2.selenium鼠标事件
3.selenium键盘事件
4.cookies操作
5.截图

selenium基本定位

基本定位方法:

定位一个元素:

  • find_element_by_id #属性id
  • find_element_by_name #属性name
  • find_element_by_link_text #用文字链接方式定位
  • find_element_by_partial_link_text #用局部文字链接定位
  • find_element_by_tag_name#属性tag
  • find_element_by_class_name #属性classname
  • find_element_by_css_selector
  • find_element_by_xpath

定位多个元素:

  • find_elements_by_id #属性id
  • find_elements_by_name #属性name
  • find_elements_by_link_text #用文字链接方式定位
  • find_elements_by_partial_link_text #用局部文字链接定位
  • find_elements_by_tag_name #属性tag
  • find_elements_by_class_name#属性classname
  • find_elements_by_css_selector
  • find_elements_by_xpath

selenium鼠标事件

  • 右键:context_click()
  • 双击:double_click()
  • 鼠标悬停:move_to_element()
    这个是重点以上的方法执行完之后必须调用.perform()方法才会真正的被执行
    鼠标事件依赖于ActionChains对象
    应该导入:`
from selenium.webdriver.common.action_chains import ActionChains

举例代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#鼠标事件依赖于ActionChains对象
from selenium.webdriver.common.action_chains import ActionChains
import time
try:
    driver=webdriver.Chrome("/usr/local/bin/chromedriver")
    driver.get("https://www.jd.com")

    elem=driver.find_element_by_link_text("手机")
    #鼠标悬停
    ActionChains(driver).move_to_element(elem).perform()
    time.sleep(3)
    #鼠标悬停老人机才能出来
    old_phone=driver.find_element_by_link_text("老人机")
    old_phone.click()
finally:
    time.sleep(3)
    # 在selenium中 关闭浏览器quit和close有什么区别
    # quit会退出浏览器进行 close会关闭浏览器句柄
    driver.quit()

键盘事件

键盘操作如RETURN

    search_element=driver.find_element_by_id("key")
    search_element.send_keys("电脑")
    search_element.send_keys(Keys.RETURN)    

cookies操作

登录后保存cookies以及下次登录登录时携带cookies登录
保存cookies到文件里:

def save_cookies(driver):
    projrct_path=os.path.dirname(os.getcwd())
    print("cwd="+os.getcwd())
    file_path=projrct_path+"/cookies/"
    if not os.path.exists(file_path):
        os.mkdir(file_path)
    #保存cookies到文件中,y以后再访问,读取文件
    cookies=driver.get_cookies()
    with open(file_path+"jd.cookies","w") as c:
   #这里必需使用dump方式写入文件,不然load的时候就会米粒问题 格式会不匹配
        json.dump(cookies,c)
    print(cookies)

从文件读取cookies:

def get_url_with_cookie():
    #首先获取项目路径,进而获得cookies文件的路径
    project_path=os.path.dirname(os.getcwd())
    file_path=project_path+"/cookies/"
    cookies_file=file_path+"jd.cookies"
    #读取到cookies信息
    jd_cookies_file=open(cookies_file,"r")
    jd_cookies_str=jd_cookies_file.readline()
    #加载cookies信息
    jd_cookies_dict=json.loads(jd_cookies_str)
    #这个地方必须先访问一下网站,然后把旧的cookies删除掉,再把我们保存的cookies添加进去
    driver.get("https://www.jd.com")
    driver.delete_all_cookies()
    for cookie in jd_cookies_dict:
        print(cookie)
        driver.add_cookie(cookie)
        time.sleep(1)
    driver.get("https://www.jd.com")

页面截图

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#鼠标事件依赖于ActionChains对象
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
def screen_shot(driver,file_path=None):
    #判断用户是否输入file_path
    if file_path==None:
        project_path=os.path.dirname(os.getcwd())
        print("project path 是"+project_path)
        file_path=project_path+"/images/"
        if not os.path.exists(file_path):
            #创建文件夹
            os.mkdir(file_path)
        image_name=time.strftime("%Y%m%d-%H%M%S",time.localtime())
        #拼接最终文件绝对路径加上文件名
        file_path=file_path+image_name+".png"
        print(file_path)
    driver.save_screenshot(file_path)
try:
    driver=webdriver.Chrome("/usr/local/bin/chromedriver")
    driver.get("https://www.jd.com")

    elem=driver.find_element_by_link_text("手机")
    #鼠标悬停
    ActionChains(driver).move_to_element(elem).perform()
    time.sleep(3)
    old_phone=driver.find_element_by_link_text("老人机")
    old_phone.click()
    driver.save_screenshot("laorenji.png")
    #第一步 获取浏览器的所有句柄
    handles=driver.window_handles
    #第二步 匹配是否是当前的句柄,如果不是,则切换句柄
    current_handle=driver.current_window_handle
    for handle in handles:
        if handle!=current_handle:
            driver.switch_to.window(handle)
            screen_shot(driver)
finally:
    time.sleep(3)
    driver.quit()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值