selenium用法入门简介,基于python编码使用

selenium
本文使用python版本:3.7.9
Chrom版本:105.0.5195.127

  1. 简介
    selenium是最广泛使用的开源Web UI(用户界面)自动化测试套件之一。Selenium支持的语言包括C#,
    JAVA,Pert,PHP,python,ruby,目前,selenium web驱动程序最受python、c#欢迎。Selenium测
    试脚本可以使用任何支持的编程语言进行编码,并且可以直接再大多数现代Web浏览器中运行。在爬虫领域,
    selenium同样是一把利器,能够解决大部分的网页反爬问题。
  2. selenium安装
    pip install selenium
    pip show selenium # 产看是否安装成功
  3. 安装浏览器驱动
    不同的浏览器需要安装不同的驱动。以下是三种常见浏览器与其对应的驱动程序下载链接
    Firefox浏览器驱动: https://github.com/mozilla/geckodriver/releases
    Chrome浏览器驱动: https://chromedriver.storage.googleapis.com/index.html
    Edge浏览器驱动: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
  4. 代码示例
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

url_csdn_home = 'https://www.csdn.net/'       # 要打开的网址
url_csdn_personal = 'https://blog.csdn.net/weixin_44940593?type=blog'

driver = webdriver.Chrome()         # 打开Chrome浏览器
# driver.set_window_size(1200, 1000)  # 设置浏览器大小
driver.get(url_csdn_home)           # 自动打开浏览器并访问url,csdn主页
driver.maximize_window()            # 浏览器全屏显示

# 浏览器的前进和后退
driver.get(url_csdn_personal)       # 自动访问csdn个人主页
sleep(2)
driver.back()                       # 浏览器后退到csdn主页
sleep(2)
driver.forward()                    # 浏览器前进到个人主页

# 在新的浏览器页面打开
js = "window.open('https://blog.csdn.net/weixin_44940593?type=blog')"
driver.execute_script(js)           # 在新的页面打开
driver.refresh()                    # 刷新页面

windows = driver.window_handles     # 获取打开多个窗口的句柄
driver.switch_to.window(windows[-1])    # 切换到当前最新的窗口

sleep(2)
text_label = driver.find_element('id', 'toolbar-search-input')   # 通过id获取定位搜索输入框
text_label.send_keys('python')                                   # 在搜索框中输入python
sleep(2)
text_label.clear()                                               # 清除搜索框中的内容
print(text_label.is_enabled())
print(text_label.get_attribute('placeholder'))

button = driver.find_element('xpath', '//*[@id="toolbar-search-button"]/span')      # 定位搜索按钮

print(button.size)                                              # 输出按钮大小
print(button.text)                                              # 输出按钮上的文本
driver.close()                                                  # 关闭浏览器


# 不自动关闭浏览器
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 将option作为参数添加到Chrome中
driver = webdriver.Chrome(chrome_options=option)

# driver.set_window_size(1200, 1000)  # 设置浏览器大小
driver.get(url_csdn_personal)           # 自动打开浏览器并访问url,csdn主页
text_label = driver.find_element('id', 'toolbar-search-input')
driver.maximize_window()            # 浏览器全屏显示
# 鼠标控制
text_label.send_keys('python')
button = driver.find_element('xpath', '//*[@id="toolbar-search-button"]/span')
# 模拟完成单机鼠标左键的操作,一般点击进入子页面等会用到,左键不需要用到ActionChains
button.click()      # 单机左键(最常用)

# print(driver.window_handles)
# driver.switch_to.window(windows[-1])
# print(driver.current_window_handle)
# for wd in driver.window_handles:
#     try:
#         driver.switch_to.window(wd)
#         c_button = driver.find_element('xpath', '//*[@id="search"]')
#         break
#     except Exception:
#         print('..........')
#
# print(c_button.text, '000000000')
# ActionChains(driver).context_click(button).perform()    # 单机右键。说明:perform(),用于执行所有ActionChains中储存的动作
# ActionChains(driver).double_click(button).perform()     # 模拟鼠标双击操作
# 定位要拖动的元素
# source = driver.find_element('xpath', 'xxx')
# # 定位目标元素
# target = driver.find_element('xpath', 'xxx')
# # 执行拖动动作
# ActionChains(driver).drag_and_drop(source, target).perform()
driver.switch_to.window(windows[0])             # 切换到csdn主页
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值