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 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 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.lookup_attr("img.central-featured-logo", "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 lookup_attr(
&self,
selector: &str,
attribute: &str
) -> Result<Option<String>, CmdError>
&self,
selector: &str,
attribute: &str
) -> Result<Option<String>, CmdError>
Look up an attribute value by name for the element matching selector
.
selector
should be a CSS selector. Ok(None)
is returned if the element does not have
the given attribute. Err(NoSuchElement)
is returned if the element could not be found.
fn lookup_prop(
&self,
selector: &str,
prop: &str
) -> Result<Option<String>, CmdError>
&self,
selector: &str,
prop: &str
) -> Result<Option<String>, CmdError>
Look up a DOM property for the element matching selector
.
selector
should be a CSS selector. Ok(None)
is returned if the element is not found, or
it does not have the given property.
fn lookup_text(&self, selector: &str) -> Result<Option<String>, CmdError>
Look up the text contents of a node matching the given CSS selector.
Ok(None)
is returned if the element was not found.
fn lookup_html(
&self,
selector: &str,
inner: bool
) -> Result<Option<String>, CmdError>
&self,
selector: &str,
inner: bool
) -> Result<Option<String>, CmdError>
Look up the HTML contents of a node matching the given CSS selector.
Ok(None)
is returned if the element was not found. inner
dictates whether the wrapping
node's HTML is excluded or not. For example, take the HTML:
<div id="foo"><hr /></div>
With inner = true
, <hr />
would be returned. With inner = false
,
<div id="foo"><hr /></div>
would be returned instead.
fn click<'a>(
&'a mut self,
selector: &str
) -> Result<Option<&'a mut Self>, CmdError>
&'a mut self,
selector: &str
) -> Result<Option<&'a mut Self>, CmdError>
Simulate the user clicking on the element matching the given CSS selector.
For convenience, Ok(None)
is returned if the element was not found.
Note that this may result in navigation.
fn click_by_text<'a>(
&'a mut self,
text: &str
) -> Result<Option<&'a mut Self>, CmdError>
&'a mut self,
text: &str
) -> Result<Option<&'a mut Self>, CmdError>
Simulate the user clicking on a link with the given text.
For convenience, Ok(None)
is returned if the element was not found.
The text matching is exact.
fn follow_link_nojs<'a>(
&'a mut self,
selector: &str
) -> Result<Option<&'a mut Self>, CmdError>
&'a mut self,
selector: &str
) -> Result<Option<&'a mut Self>, CmdError>
Follow the href
target of the element matching the given selector without causing a
click interaction.
For convenience, Ok(None)
is returned if the element was not found, or if it does not
have an href
attribute.
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.