Схемы, примеры, и варианты кода. В колонке Selenide даны несколько наиболее удобных способов.
| Rule | Schema | Example | Comment | Selenide (варианты) | Selenium |
|---|---|---|---|---|---|
| tag & attribute | //tag[@attribute="value"] |
//input[@placeholder="Username"] |
Находит элемент по тэгу и атрибуту |
$(byXpath("//input[@placeholder='Username']"))
|
driver.findElement(By.xpath("//input[@placeholder='Username']"))
|
| text() | //tag[text()="value"] |
//button[text()="Login"] |
Полное совпадение текста |
$(byText("Login"))$$("button").findBy(text("Login"))$(byXpath("//button[text()='Login']"))
|
driver.findElement(By.xpath("//button[text()='Login']"))
|
| contains() (text) | //tag[contains(text(),"value")] |
//div[contains(text(),"Error")] |
Частичное совпадение текста |
$(withText("Error"))Генерирует примерно такой XPath: .//*[contains(text(),'Error')] $(byXpath("//div[contains(text(),'Error')]"))
|
driver.findElement(By.xpath("//div[contains(text(),'Error')]"))
|
| contains() (attr) | //tag[contains(@attribute,"value")] |
//input[contains(@placeholder,"Usern")] |
Частичное совпадение значения атрибута |
$(byXpath("//input[contains(@placeholder,'Usern')]"))
|
driver.findElement(By.xpath("//input[contains(@placeholder,'Usern')]"))
|
| multiple attributes | //tag[rule1][rule2] |
//input[@type="text"][@name="login"] |
Несколько условий |
$("input[type='text'][name='login']")$(byCssSelector("input[type='text'][name='login']"))$(byXpath("//input[@type='text'][@name='login']"))
|
driver.findElement(By.xpath("//input[@type='text'][@name='login']"))
|
| moving through the tree | //parent/child |
//div/span |
Переход родитель → потомок |
$("div span")$(byXpath("//div/span"))
|
driver.findElement(By.xpath("//div/span")) |
| not() | //tag[not(contains(@attr,"v"))] |
//input[not(contains(@type,"hidden"))] |
Исключение по условию |
$(byXpath("//input[not(contains(@type,'hidden'))]"))
|
driver.findElement(By.xpath("//input[not(contains(@type,'hidden'))]"))
|
| indexing | (//tag[@attr="v"])[N] |
(//input[@type="text"])[2] |
N‑й элемент в выборке |
$$("input[type='text']").get(1)$(byXpath("(//input[@type='text'])[2]"))
|
driver.findElement(By.xpath("(//input[@type='text'])[2]"))
|
| parent | //tag/.. |
//input/.. |
Переход к родителю | $(byXpath("//input/..")) |
driver.findElement(By.xpath("//input/..")) |
| following-sibling | //tag/following-sibling::tag2 |
//label/following-sibling::input |
Сосед справа на том же уровне | $(byXpath("//label/following-sibling::input")) |
driver.findElement(By.xpath("//label/following-sibling::input"))
|
| preceding-sibling | //tag/preceding-sibling::tag2 |
//input/preceding-sibling::label |
Сосед слева | $(byXpath("//input/preceding-sibling::label")) |
driver.findElement(By.xpath("//input/preceding-sibling::label"))
|
| ancestor | //tag/ancestor::X |
//span/ancestor::form |
Любой предок выше по дереву | $(byXpath("//span/ancestor::form")) |
driver.findElement(By.xpath("//span/ancestor::form"))
|
| Rule | Schema | Example | Comment | Selenide (варианты) | Selenium |
|---|---|---|---|---|---|
| tag & attribute | tag[attribute="value"] |
input[aria-label="Search"] |
По атрибуту |
$("input[aria-label='Search']")$(byAttribute("aria-label","Search"))$(byCssSelector("input[aria-label='Search']"))
|
driver.findElement(By.cssSelector("input[aria-label='Search']"))
|
| id | #id |
#loginBtn |
По id |
$("#loginBtn")$(byId("loginBtn"))
|
driver.findElement(By.id("loginBtn")) |
| class name | .className |
.btn-primary |
По классу |
$(".btn-primary")$(byClassName("btn-primary"))
|
driver.findElement(By.className("btn-primary"))
|
| multiple class names | .class1.class2 |
.btn.large |
Два класса одновременно |
$(".btn.large")$(byCssSelector(".btn.large"))
|
driver.findElement(By.cssSelector(".btn.large"))
|
| attribute *= | [attribute*="value"] |
input[placeholder*="user"] |
Атрибут содержит |
$("input[placeholder*='user']")$(byAttribute("placeholder","user"))
|
driver.findElement(By.cssSelector("input[placeholder*='user']"))
|
| attribute ^= | [attribute^="value"] |
input[placeholder^="user"] |
Атрибут начинается с |
$("input[placeholder^='user']")$(byCssSelector("input[placeholder^='user']"))
|
driver.findElement(By.cssSelector("input[placeholder^='user']"))
|
| attribute $= | [attr$="value"] |
img[src$=".png"] |
Атрибут заканчивается на |
$("img[src$='.png']")$(byCssSelector("img[src$='.png']"))
|
driver.findElement(By.cssSelector("img[src$='.png']"))
|
| name / link text (полезно знать) | a:contains(text) - (нет в чистом CSS) |
Ссылки по тексту |
В CSS нет поиска по тексту - используем API |
$(byLinkText("Home"))$(byPartialLinkText("Hom"))
|
driver.findElement(By.linkText("Home"))driver.findElement(By.partialLinkText("Hom"))
|
Импорты (Selenide):
import static com.codeborne.selenide.Selenide.*;,
import static com.codeborne.selenide.Selectors.*;,
import static com.codeborne.selenide.Condition.*;