分享python+Selenium测试工具模拟浏览器检索数据

本文介绍了如何使用Python的Selenium库配合ChromeDriver进行网页自动化测试,包括设置Chrome的无界面模式、禁用JavaScript、设置用户代理等,以及处理Chrome版本兼容性和页面抓取的详细步骤。

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

执行JS的类库:execjs,PyV8,selenium,node

pip list

c6a7a6b08a9f42f59435f2a41dfb9ab5.png

pip install selenium

2420b497f48b44d3b0af2310d399e805.png

pip install xlrd

3524f152d1a545ca8390a8541c409947.png

pip install xlwt

166d8ef184e4466ebf3635805f940570.png

pip install PyExecJS

388270bbce66455d8ec049205ad4e9f9.png

pip install xlutils

9d54006a2af8474cae64938faef17776.png

252d3a76866241c293e41ede8da8fb8b.png

selenium测试工具可以用来模拟用户浏览器的操作,其支持的浏览器有:PhantomJS,Firefox,Chrome等等,开发者可以根据当前的系统形式选择不同的模拟浏览器。
每种模拟浏览器都需要对应的浏览器驱动(一个以.exe为后缀的可执行文件),使用谷歌浏览器Chrome,对应的浏览器驱动可以通过下面的网址下载。要完整地安装Python-Selenium库,让Chrome浏览器实现自动化,需要完成下面4步:Chromedriver安装、Selenium库安装、测试、关闭Chrome浏览器自动更新。

ChromeDriver - WebDriver for Chrome - Downloads

如果您使用的是Chrome 115或更新版本,请参阅Chrome测试可用性仪表板。此页面为特定的ChromeDriver版本提供下载。

624ad2710afc4e209917ff56e0ecd689.png

a42042c6bc6c4f599d475ef2397ad324.png

# -*- coding: utf-8 -*-
"""
Created on Thu Feb 24 16:10:55 2024
@author: Administrator
"""

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
 
import threading
from datetime import datetime
from queue import Queue
 
from xlrd import open_workbook
from xlutils.copy import copy

import random
import time

import re


class ShellChrome(object):
   def __init__(self,count):
       self.count = count
       self.que = Queue(maxsize = count)
       ua = self.getheaders()
       #mobile_emulation = {"deviceName": "Nexus 7"}
       self.options = webdriver.ChromeOptions()
       # 把Chrome设置成可视化无界面模式,windows/Linux 皆可
       self.options.add_argument('headless')
       # 转换手机模式
       #self.options.add_experimental_option("mobileEmulation", mobile_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

:MNongSciFans

抛铜币以舒赞同,解兜囊以现支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值