Rust 氧化铬库 - 如何点击浏览器后退按钮?

问题描述 投票:0回答:1

假设我使用了 Rust 中的chromiumoxy库来访问几个网页:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    
    // create a `Browser` that spawns a `chromium` process running with UI (`with_head()`, headless is default) 
    // and the handler that drives the websocket etc.
    let (mut browser, mut handler) =
        Browser::launch(BrowserConfig::builder().with_head().build()?).await?;
    
    // spawn a new task that continuously polls the handler
    let handle = tokio::task::spawn(async move {
        while let Some(h) = handler.next().await {
            if h.is_err() {
                break;
            }
        }
    });
    
    // create a new browser page and navigate to the url
    let page = browser.new_page("https://en.wikipedia.org").await?;
    page.wait_for_navigation().await?;

    // go to another url
    page.goto("https://www.example.com/").await?;
    page.wait_for_navigation().await?;

    // how do I now click the back button on the browser to go to the previous url?
    

    browser.close().await?;
    handle.await; 

    Ok(())

}

现在如何单击浏览器上的后退按钮访问上一页?

(我知道我可以使用

page.goto()
作为替代方案,但这不是我想要的。)

rust
1个回答
0
投票

添加 Page::go_back

Page::go_forward
 目前是一个 
未解决的问题,但您可以使用原始
Command
execute
:

自行实现它们
trait PageExt {
    async fn go_back(&self) -> Result<CommandResponse<NavigateToHistoryEntryReturns>>;
}

impl PageExt for Page {
    async fn go_back(&self) -> Result<CommandResponse<NavigateToHistoryEntryReturns>> {
        let CommandResponse {
            result:
                GetNavigationHistoryReturns {
                    current_index,
                    entries,
                },
            ..
        } = self.execute(GetNavigationHistoryParams {}).await?;
        self.execute(NavigateToHistoryEntryParams {
            entry_id: entries[current_index as usize - 1].id,
        })
        .await
    }
}

use chromiumoxide::cdp::browser_protocol::page::{
    GetNavigationHistoryParams, GetNavigationHistoryReturns, NavigateToHistoryEntryParams,
    NavigateToHistoryEntryReturns,
};
use chromiumoxide::{error::Result, Page};
use chromiumoxide_types::CommandResponse;
© www.soinside.com 2019 - 2024. All rights reserved.