Manual
Getting Started
You should first find a remote web driver for the package to connect.
For example, you could use a Docker image for spinning a container for a remote web driver such as
docker run -d -p 4444:4444 --name selenium selenium/standalone-chrome
which will run the Selenium standalone Google Chrome web driver.
We can connect to it from WebDriver.jl through the following steps,
We specify the requested web driver capabilities. The required argument being the browser name (chrome
).
using WebDriver
capabilities = Capabilities("chrome")
Remote WebDriver Capabilities
browserName: chrome
We specify the remote driver we want to connect to,
The host will likely be localhost
or the name of the container if in a container network (e.g., selenium
).
The port will be that being exposed which by default is 4444
. In the example, above we mapped the container to listen at the port 4444
.
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
Remote WebDriver
Lastly, we can start a session in the webdriver.
status(wd) # Will check if server is running and available for new sessions
true
sessions(wd) # All active sessions
0-element Array{String,1}
session = Session(wd) # Will create a new session
Session
The session we just created is now active
sessions(wd)
1-element Array{String,1}:
"9e39c704d1c8a4ab629f315617699a49"
After performing operations with that session, you may close it with
delete!(session)
"9e39c704d1c8a4ab629f315617699a49"
We can confirm that the session was actually deleted.
sessions(wd)
0-element Array{String,1}
For the following section, feel free to follow up the session actions in your browser.
Navigation
using WebDriver
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
We can query the current URL
current_url(session)
"data:,"
We can navigate to a different URL
navigate!(session, "http://thedemosite.co.uk/addauser.php")
current_url(session)
"http://thedemosite.co.uk/addauser.php"
We can go back
back!(session)
current_url(session)
"data:,"
We can go forward
forward!(session)
current_url(session)
"http://thedemosite.co.uk/addauser.php"
We can refresh
refresh!(session);
Elements
using WebDriver
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
We can find the active element
active_element(session)
Element
We can find an element based on css selector
, link text
, partial link text
, tag name
or xpath
navigate!(session, "http://book.theautomatedtester.co.uk/chapter1")
selecttype = Element(session, "xpath", """//select[@id='selecttype']""") # Find first element
Element
selecttypes = Elements(session, "xpath", """//select[@id='selecttype']""") # Find all elements
1-element Array{Element,1}:
Element
t1selenium = Element(selecttype, "xpath", """//option[@value='Selenium IDE']""") # Find Element From Element
Element
tsselenium = Elements(selecttype, "xpath", """//option""") # Find Elements from Element
4-element Array{Element,1}:
Element
Element
Element
Element
We can query elements for attributes, properties, tag, text, css values, dimensions, etc.
element_attr(t1selenium, "value") # Get Element Attribute
"Selenium IDE"
element_property(t1selenium, "value") # Get Element Property
"Selenium IDE"
element_css(selecttype, "font-size") # Get Element CSS Value
"13.3333px"
element_text(t1selenium) # Get Element Text
"Selenium IDE"
element_tag(selecttype) # Get Element Tag Name
"select"
rect(selecttype) # Get Element Rect
(width = 111, height = 19, x = 8, y = 181)
isenabled(selecttype) # Is the element enabled
true
Clicking elements
radiobutton = Element(session, "xpath", """//input[@id='radiobutton']""")
Element
element_property(radiobutton, "checked")
false
click!(radiobutton) # Element Click
element_property(radiobutton, "checked")
true
Text boxes
text_box = Element(session, "xpath", """//*[@id='html5div']""")
Element
element_text(text_box)
"To be used after the AJAX section of the book"
clear!(text_box) # Element Clear
element_text(text_box)
""
element_keys!(text_box, "All your base are belong to us") # Element Send Keys
element_text(text_box)
"All your base are belong to us"
Page Source
using WebDriver, Gumbo, Cascadia
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
navigate!(session, "http://book.theautomatedtester.co.uk/chapter1")
html = parsehtml(source(session))
eachmatch(Selector("#selecttype"), html.root)
1-element Array{Gumbo.HTMLNode,1}:
Gumbo.HTMLElement{:select}:<select id="selecttype">
<option value="Selenium IDE">
Selenium IDE
</option>
<option value="Selenium Code">
Selenium Core
</option>
<option value="Selenium RC">
Selenium RC
</option>
<option value="Selenium Grid">
Selenium Grid
</option>
</select>
Execute Script
using WebDriver, Gumbo, Cascadia
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
navigate!(session, "http://book.theautomatedtester.co.uk/chapter1")
selecttype = Element(session, "xpath", """//select[@id='selecttype']""")
element_attr(selecttype, "value") == "Selenium IDE"
true
script!(session, "arguments[0].value = arguments[1];", selecttype, "Selenium Grid")
element_attr(selecttype, "value")
"Selenium Grid"
Cookies
using WebDriver, Gumbo, Cascadia
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
Find all cookies
navigate!(session, "http://book.theautomatedtester.co.uk/chapter8")
length(cookies(session))
1
Get Named Cookie
second_cookie = Element(session, "xpath", "//input[@id='secondCookie']")
click!(second_cookie)
cookie(session, "visitorCount").name
"visitorCount"
Add a cookie
cookie!(session, Cookie("WhoIsAwesome", "ME!"))
cookie(session, "WhoIsAwesome").value
"ME!"
Delete Cookie
delete!(session, "WhoIsAwesome") # Deletes cookie of that name
delete!(session, "") # Deletes all cookies
length(cookies(session))
0
More Advanced Features
This will go over user prompts, hoverover / move pointer to, changing frames, and screenshots
Let us first hover over and accept a user-prompt.
using WebDriver
capabilities = Capabilities("chrome")
wd = RemoteWebDriver(capabilities, host = ENV["WEBDRIVER_HOST"], port = parse(Int, ENV["WEBDRIVER_PORT"]))
session = Session(wd)
Session
navigate!(session, "http://book.theautomatedtester.co.uk/chapter4")
hoverOver = Element(session, "xpath", "//*[@id='hoverOver']")
moveto!(hoverOver)
alert_text(session)
"on MouseOver worked"
accept(session) # dismiss(session) would have also worked
We can also switch frames as in the following example,
navigate!(session, "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt")
button = Element(session, "css selector", "body > div.trytopnav > div > button")
click!(button)
example_frame = Element(session, "xpath", "//*[@name='iframeResult']")
frame!(example_frame)
button = Element(session, "xpath", "/html/body/button")
click!(button)
alert_text(session)
"Please enter your name"
alert_text!(session, "Nosferican")
accept(session)
my_name = Element(session, "xpath", "//*[@id='demo']")
element_text(my_name)
"Hello Nosferican! How are you today?"
using Base64
ss = write(joinpath(@__DIR__, "img.png"), base64decode(screenshot(session)))
72314
More Features
For more features, take a look at the API.