chromedriver(python+selenium)高级操作

环境

Python 3.8.6
Selenium 3.8.1
Chromedriver 103.0.5060.24
Windows 11

解释selenium操控chrome高级操作

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options

import unittest, time, re
class DownLoad(unittest.TestCase):
    def setUp(self):
        options = webdriver.ChromeOptions()
        out_path = r""
        options.add_experimental_option("prefs", {
            "profile.default_content_settings.popups": 0,	# 默认设置窗口
            "download.default_directory": out_path,				# 默认目录
            "download.prompt_for_download": False,				# 是打开询问保存文位置
            "download.directory_upgrade": True,						# 目录升级
            "safebrowsing.enabled": False								# 安全浏览启用
        })
        # options.add_argument('--user-data-dir=/Users/user_data')
        options.add_argument('--user-data-dir=D:\\KDT_test\\userdata')	# 保存当前状态
        self.driver = webdriver.Chrome(chrome_options=options)
        self.driver.implicitly_wait(30)
        self.accept_next_alert = True
    def test_download(self):
        driver = self.driver
        driver.get("https://adaad")
        driver.find_element(By.ID, "UserID").click()
        driver.find_element(By.ID, "UserID").send_keys("username")
        driver.find_element(By.ID, "Paswd").click()
        driver.find_element(By.ID, "Paswd").send_keys("passwoed")
        driver.find_element(By.ID, "Paswd").send_keys(Keys.ENTER)
        driver.find_element(By.LINK_TEXT, "down_load").click()
        driver.switch_to.frame(1)
        driver.find_element(By.LINK_TEXT, "导出").click()
        time.sleep(5)
        driver.quit()

            ChromeOptions options = new ChromeOptions();
            options.BinaryLocation = sysPath + "/Chrome/Chrome.exe";                             ---------设置浏览器程序位置
            options.AddArgument("url=data:,");                                                                     ----------- 设置启动浏览器空白页
            options.AddArgument("--headless");                                                                   ----------- 隐藏浏览器
            options.SetLoggingPreference("off", LogLevel.Off);                                           ------------- 禁止 console.log 日志输出
            IWebDriver driver = new ChromeDriver(sysPath + "/Chrome/", options);          -------------- 设置chromedriver.exe 文件夹路径
 
 
 
chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
 
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
 
详细参数:https://peter.sh/experiments/chromium-command-line-switches/
        https://www.cnblogs.com/c-x-a/p/8572457.html
        https://www.cnblogs.com/yangjintao/p/10599868.html
        https://blog.csdn.net/qq_26200629/article/details/86141131
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python Selenium是一个自动化测试工具,用于模拟用户在Web应用程序中的操作。下面是搭建Python Selenium环境的步骤: 1. 安装Python:首先,需要从Python官方网站下载并安装Python解释器。选择最新版本的Python,并按照安装向导完成安装过程。 2. 安装pip:pip是Python的软件包管理器,用于安装第三方库。在安装Python过程中,一般会默认安装pip。可以在命令行中输入"pip"来验证其是否成功安装。 3. 安装Selenium库:使用pip命令来安装Selenium库,命令如下: ``` pip install selenium ``` 4. 下载Web驱动程序:Selenium需要与浏览器进行交互,因此需要下载相应的Web驱动程序。不同浏览器需要不同的驱动程序。常用的浏览器驱动如下: - Chrome: ChromeDriver - Firefox: GeckoDriver - Safari: SafariDriver - Edge: MicrosoftWebDriver 下载对应浏览器的Web驱动程序,并将其添加到系统路径中,以便Selenium能够找到驱动程序。 5. 编写Python脚本:使用任何文本编辑器创建一个Python脚本,导入Selenium库,并编写自动化测试的代码逻辑。例如,打开浏览器、访问网页、查找元素、输入文本等等。 6. 运行脚本:在命令行中运行Python脚本,确认环境配置正确并且脚本能够执行。 以上就是搭建Python Selenium环境的主要步骤。通过按照上述步骤安装所需的软件和库,并编写脚本,就可以使用Python Selenium进行自动化测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yuno Wang

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值