当前位置: 首页 > article >正文

Rust Web自动化Demo

1.新建项目

使用RustCover新建项目,目录如下:

Cargo.toml文件

[package]
name = "Demo"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1", features = ["full"] }
thirtyfour = { version = "0.31.0", features = ["component"] }
scraper = "0.15.0"

2.编写代码

以百度网站为例

use std::time::Duration;
use thirtyfour::By;
use tokio;
use thirtyfour::prelude::{DesiredCapabilities, WebDriver, WebDriverResult};

#[tokio::test]
async fn navigate_to_url() -> WebDriverResult<()> {
    let capabilities = DesiredCapabilities::chrome();
    let driver = WebDriver::new("http://localhost:9515", capabilities)
        .await
        .expect("Failed to connect to localhost:9515!");
    driver.maximize_window().await?;
    driver.set_implicit_wait_timeout(std::time::Duration::from_secs(10)).await?;
    tokio::time::sleep(Duration::from_secs(3)).await;
    driver
        .goto("https://www.baidu.com")
        .await
        .expect("Couldn't navigate to the URL!");
    tokio::time::sleep(Duration::from_secs(2)).await;
    let search_input = driver.find(By::Id("kw")).await?;
    search_input.send_keys("Hello").await?;
    tokio::time::sleep(Duration::from_secs(2)).await;
    let search_button = driver.find(By::Id("su")).await?;
    search_button.click().await?;
    tokio::time::sleep(Duration::from_secs(2)).await;
    let page_title = driver.title()
        .await
        .expect("Page title not found");
    driver.quit().await?;
    assert_eq!(
        page_title,
        "Hello_百度搜索"
    );
    Ok(())
}

3.运行项目

在RustCover打开终端,输入命令:

chromedriver --port=9515

然后再新建终端,输入命令:

cargo test

http://www.kler.cn/news/326432.html

相关文章:

  • 《动手学深度学习》笔记2.4——神经网络从基础→进阶 (文件读写-保存参数和模型)
  • 堆的数组实现
  • nginx的安装和使用
  • 网页前端开发之Javascript入门篇(1/9):变量
  • 千益畅行,旅游创业新模式的创新与发展
  • 【Python报错已解决】ModuleNotFoundError: No module named ‘tensorflow‘
  • [每周一更]-(第117期):硬盘分区表类型:MBR和GPT区别
  • Windows开发工具使用技巧大揭秘:让编码效率翻倍的秘籍!
  • 软件设计之SSM(3)
  • SpringBoot中各种O的分层模型
  • 16 数组——18. 四数之和 ★★
  • 6种MySQL高可用方案对比分析
  • CleanMyMac X v4.12.1 中文破解版 Mac优化清理工具
  • 10个降低性能的SQL问题及改进措施
  • Leetcode面试经典150题-201.数字范围按位与
  • oracle 分表代码示例
  • FiBiNET模型实现推荐算法
  • qiankun自定义数据通信方案
  • Json files to Excel - Python
  • 【QT】QWidget 重要属性
  • Golang | Leetcode Golang题解之第435题无重叠区间
  • Qt开发第一讲
  • ip池子的大小与什么相关?
  • echarts 导出pdf空白原因
  • Spring系统学习(五)——Spring数据库编程
  • YOLOv8改进 | 主干改进篇,华为的轻量化架构GhostNetV2改进特征提取网络
  • 前端项目依赖包中的依赖包漏洞解决方案
  • springboot实战学习(10)(ThreadLoacl优化获取用户详细信息接口)(重写拦截器afterCompletion()方法)
  • 解决VRM格式模型在Unity中运行出现头发乱飞等问题
  • Java | Leetcode Java题解之第443题压缩字符串