web-driver.Searchable module

Summary

It provides features that search element by some retrieval methods.

You can specify how to search the element as below.

Methods

It's an alias of xpath_search. This method exist fot compatibility with XMLua.Searchable:search()

It searches elements by css selector and returns as web-driver.ElementSet object.

Example:

local web_driver = require("web-driver")
local driver = web_driver.Firefox.new()

local URL =
  "https://clear-code.gitlab.io/lua-web-driver/sample/"

driver:start_session(function(session)
  session:navigate_to(URL)

  local elements = session:css_select('p')
  for _,v in pairs(elements:texts()) do
    print(v)
-- Hello 1
-- Hello 2
-- Hello 3
  end
end)

It searches elements by XPath and returns as web-driver.ElementSet object.

Example:

local web_driver = require("web-driver")
local driver = web_driver.Firefox.new()

local URL =
  "https://clear-code.gitlab.io/lua-web-driver/sample/"

driver:start_session(function(session)
  session:navigate_to(URL)

  local elements = session:search('/html/body/p')
  print(elements[1]:text())
  -- 1
end)

It searches elements by link search and returns as web-driver.ElementSet object.

Example:

local web_driver = require("web-driver")
local driver = web_driver.Firefox.new()

local URL =
  "https://clear-code.gitlab.io/lua-web-driver/sample/link.html"

driver:start_session(function(session)
  session:navigate_to(URL)

  local links = session:link_search("1")
  links[1]:click()
  local elements = session:css_select("p")
  print(elements:text())
  -- 1
end)

It searches elements by partial link search and returns as web-driver.ElementSet object.

Example:

local web_driver = require("web-driver")
local driver = web_driver.Firefox.new()

local URL =
  "https://clear-code.gitlab.io/lua-web-driver/sample/announcement.html"

driver:start_session(function(session)
  session:navigate_to(URL)

  local links = session:partial_link_search("anno")
  links[1]:click()
  local elements = session:css_select("p")
  print(elements:text())
  -- 1
end)

It searches elements by tag name and returns as web-driver.ElementSet object.

Example:

local web_driver = require("web-driver")
local driver = web_driver.Firefox.new()

local URL =
  "https://clear-code.gitlab.io/lua-web-driver/sample/1.html"

driver:start_session(function(session)
  session:navigate_to(URL)

  local p = session:tag_name("p")
  print(p[1]:text())
  -- 1
end)

See also