Struct fantoccini::Client
[−]
[src]
pub struct Client { /* fields omitted */ }
A WebDriver client tied to a single browser session.
Methods
impl Client
[src]
fn new<U: IntoUrl>(webdriver: U) -> Result<Self, NewSessionError>
Create a new Client
associated with a new WebDriver session on the server at the given
URL.
fn set_ua<S: Into<String>>(&mut self, ua: S)
Set the User Agent string to use for all subsequent requests.
fn goto<'a>(&'a mut self, url: &str) -> Result<&'a mut Self, CmdError>
Navigate directly to the given URL.
fn current_url(&self) -> Result<Url, CmdError>
Retrieve the currently active URL for this session.
fn source(&self) -> Result<String, CmdError>
Get the HTML source for the current page.
fn execute(&mut self, script: &str, args: Vec<Json>) -> Result<Json, CmdError>
Execute the given JavaScript script
in the current browser session.
args
is available to the script inside the arguments
array. Since Element
implements
ToJson
, you can also provide serialized Element
s as arguments, and they will correctly
serialize to DOM elements on the other side.
fn raw_client_for<'a>(
&'a mut self,
method: Method,
url: &str
) -> Result<RequestBuilder<'a>, CmdError>
&'a mut self,
method: Method,
url: &str
) -> Result<RequestBuilder<'a>, CmdError>
Get a hyper::RequestBuilder
instance with all the same cookies as the current session has
for the given url
.
The RequestBuilder
can then be used to fetch a resource with more granular control (such
as downloading a file).
Note that the client is tied to the lifetime of the client to prevent the Client
from
navigating to another page. This is because it would likely be confusing that the builder
did not also navigate. Furthermore, the builder's cookies are tied to the URL at the time
of its creation, so after navigation, the user (that's you) may be confused that the right
cookies aren't being included (I know I would).
Examples
use fantoccini::Client; let mut c = Client::new("http://localhost:4444").unwrap(); c.goto("https://www.wikipedia.org/").unwrap(); let img = c.by_selector("img.central-featured-logo").unwrap() .attr("src").unwrap().unwrap(); let raw = c.raw_client_for(fantoccini::Method::Get, &img).unwrap(); let mut res = raw.send().unwrap(); use std::io::prelude::*; let mut pixels = Vec::new(); res.read_to_end(&mut pixels).unwrap(); println!("Wikipedia logo is {}b", pixels.len());
fn by_selector<'a>(
&'a mut self,
selector: &str
) -> Result<Element<'a>, CmdError>
&'a mut self,
selector: &str
) -> Result<Element<'a>, CmdError>
Find an element by CSS selector.
fn by_link_text<'a>(&'a mut self, text: &str) -> Result<Element<'a>, CmdError>
Find an element by its link text.
The text matching is exact.
fn by_xpath<'a>(&'a mut self, xpath: &str) -> Result<Element<'a>, CmdError>
Find an element using an XPath expression.
fn wait_for<'a, F>(&'a mut self, is_ready: F) -> &'a mut Self where
F: FnMut(&Client) -> bool,
F: FnMut(&Client) -> bool,
Wait for the given function to return true
before proceeding.
This can be useful to wait for something to appear on the page before interacting with it.
While this currently just spins and yields, it may be more efficient than this in the
future. In particular, in time, it may only run is_ready
again when an event occurs on
the page.
Wait for the page to navigate to a new URL before proceeding.
If the current
URL is not provided, self.current_url()
will be used. Note however that
this introduces a race condition: the browser could finish navigating before we call
current_url()
, which would lead to an eternal wait.
fn form<'a>(&'a mut self, selector: &str) -> Result<Form<'a>, CmdError>
Locate a form on the page.
Through the returned Form
, HTML forms can be filled out and submitted.