selenium 自动化常用操作

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import random

import time

用来待时间操作

def wait(n):

time.sleep(random.randint(1, n))

实例化参数对象

options = webdriver.ChromeOptions()

加入参数, 使用options.add_argument()

常用参数

options.add_argument("--headless") # 无头, 也就是没有界面。

options.add_argument("--start-maximized") # 打开时页面最大化

保留用户数据存储的路径,有些需要保存登陆信息, 可以使用命令行启动chrome,输入用户信息保存,再运行自动化。

import os

exe_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe" -remote-debugging-port=9015 --user-data-dir=E:\wx_wifi\chrome-userdata'

os.system(exe_path)

options.add_argument(r"--user-data-dir=F:\chrome_data")

有些网站会使用反爬技术识别,是不是webdriver自动化的, 可以使用先人工手动打开chrome, 再通过端口运行,这样可以逃避识别。 参考上述exe_path的命令打开。

options.add_experimental_option("debuggerAddress", "127.0.0.1:9015")

它允许用户在Chrome浏览器中启动浏览器时排除特定的Chrome命令行开关,enable-automation 是一个Chrome命令行开关,用于启用Chrome浏览器的自动化功能。禁用它可以帮助防止被一些自动化检测工具检测到。

options.add_experimental_option('excludeSwitches', ['enable-automation'])

实例化chrome驱动对象, 指定驱动的位置与参数

driver = webdriver.Chrome(r'./chromedriver-122.exe', options=options)

设置implicitly_wait,implicitly_wait 的超时时间是从 WebDriver实例化后就开始计算的,直到找到元素或超时为止。如果在超时时间内元素仍然没有加载完成,WebDriver 会抛出一个异常。 只对find_element, find_elements方法的调用。

driver.implicitly_wait(60*3)

打开网页

driver.get('https://baidu.com')

-------------

查找标签元素

返回单个WebElement对象

wait(5)

element = driver.find_element_by_xpath('//input[@id="account"]')

返回列表, 列表元素为WebElement对象

element = driver.find_elements_by_xpath('/html')

-------------

点击元素, 按钮所在的标签, 所含的元素包含里面的都没有问题, 会在查找到元素的"正中心"点击

element.click()

输入框, 先清除文字,后输入文字。

element.clear()

element.send_keys('要输入的字符串')

查看元素的文本内容(可以html界面上展示的文本内容, <> 这里面的内容 <>)

print(element.text)

获取输入框里面的文字

print(element.get_attribute('value'))

如果有些元素没有展示在界面上,text可能获取不了的, 可以用 innerText 或者textContent

print(element.get_attribute('innerText'))

查看元素属性的值, 这个不在html界面显示, 但可以查看源码看到。

print(element.get_attribute('placeholder'))

获取整个元素(包含父元素)里面的元素对应的HTML文本内容, outerHTML

print(element.get_attribute('outerHTML'))

获取单个元素里面的元素对应的HTML文本内容, innerHTML 与 outerHTML 只差一个父元素的显示。

element = driver.find_element_by_xpath('//div[@class="country-panel-code"]')

print(element.get_attribute('innerHTML'))

键盘操作 全选, 复制, 贴贴

element = driver.find_element_by_xpath('//input[@id="account"]')

wait(2)

element.send_keys(Keys.CONTROL, 'a')

wait(2)

element.send_keys(Keys.CONTROL, 'c')

element = driver.find_element_by_xpath('//input[@id="pwd"]')

element.send_keys(Keys.CONTROL, 'v')

相关推荐
(时光煮雨)8 小时前
【Python进阶】Python爬虫-Selenium
爬虫·python·selenium
依旧很淡定2 天前
Selenium(Python)创建Chrome浏览器实例
chrome·python·selenium
加油20193 天前
爬虫框架: selenium API使用介绍
爬虫·selenium·测试工具
shelter -唯4 天前
基于selenium库的爬虫实战:京东手机数据爬取
爬虫·python·selenium
深蓝电商API7 天前
实战破解前端渲染:当 Requests 无法获取数据时(Selenium/Playwright 入门)
前端·python·selenium·playwright
卓码软件测评7 天前
第三方软件验收测试:【AutoIt与Selenium结合测试文件上传/下载等Windows对话框】
windows·功能测试·selenium·测试工具·性能优化·可用性测试
最好的我们!7 天前
解决selenium的EdgeOptions addArguments is not supported问题
selenium·测试工具
万粉变现经纪人8 天前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
gc_22998 天前
学习Python中Selenium模块的基本用法(18:使用ActionChains操作鼠标)
python·selenium
paid槮9 天前
selenium完整版一览
selenium·测试工具