Skip to content

inkroom/selenium-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

selenium

selenium的rust实现

为何有此项目

  • crates.io上目前(2024-12)存在的两个项目(selenium-rs,selenium_webdriver),都是好几年前发了几个版本后就没动静了;
  • 而且都存在一个问题,就是driver程序需要另外单独启动,没有集成到程序中,甚至用于通信的http地址都是写死在代码里的

browser driver

目前只测试了firefox、chrome、edge

Browser Component
Chrome chromedriver(.exe)
Internet Explorer IEDriverServer.exe
Edge MicrosoftWebDriver
Firefox geckodriver(.exe)
Opera operadriver(.exe)
Safari safaridriver

use

引入

cargo add selenium --git https://github.com/inkroom/selenium-rs

镜像

selenium会从github上下载一个js文件,如果遇到download from github fail提示,可以手动下载后,使用 MIRROR_JS_FILE环境变量指明文件的绝对路径

启动

使用本地driver

let option = FirefoxBuilder::new()
    .driver(
        format!(
            "{}/geckodriver",
            std::env::current_dir()
                .map_err(|f| SError::Message(f.to_string()))
                .unwrap()
                .display()
        )
        .as_str(),
    )
    .build();

let d = Driver::new(option).unwrap();
d.get("https://github.com").unwrap();

目前每一个Driver都会启用一个新的子进程,所以项目中最好只有一个Driver对象

使用远程server

let option = FirefoxBuilder::new().url("http://127.0.0.1:3824").build();
let d = Driver::new(option).unwrap();
d.get("https://github.com").unwrap();

查找元素

    driver.find_element(By::Css("#id"));

键盘操作

普通的输入,比如input输入可以

driver.find_element(By::Css("#input")).unwrap().send_keys("input").unwrap()

更复杂的操作,比如组合键

driver.actions()
      .key_down_special(Key::Control)
      .key_down("e")
      .key_up("e")
      .key_up_special(Key::Control)
      .perform()
      .unwrap();

About

a selenium library for rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published