Skip to main content

Web elementų išrinkimas

M.Gzegozevskis...About 2 minTestingSeleniumTestavimas

Selenium lokatoriai (programiškai)

Kas yra WebDriver ir WebElement?

  • WebDriver - tai testinės aplinkos naršyklė su galimybę ją valdyti naudojant jai pritaikytus draiverius, kuriuos kuria ir viešai dalinasi naršyklių savininkai.
  • WebElement - yra įprastas HTML DOM (Document Object Model) elementas pvz: Nuoroda, pavadinimas, įvesties laukas, blokas, mygtukas, paveikslėlis ir t.t.
  • Pavyzdžiui naudojant metodus aprašytus Selenium įrankio galima išrinkti su Java kalba aprašytu sakiniu WebElement cheese = findElement(By.idopen in new window("cheese)); gražina elementą pagal nurodytą id atributą.
Pagal našumąLokatoriusAprašymas
vidutinisclass nameIeškoti pagal tekstą aprašytą prie class atributo.
WebElement cheese = driver.findElement(By.className("cheese"));
vidutiniscss selectorIeškoti pagal nurodytą CSS selektorių. Pasidomėti kas yra tas CSS selektoriusopen in new window.
WebElement pass= driver.findElement(By.cssSelector("input #pass"));
geriausiasidIeškoti elementų pagal nurodytą id atributą, kadangi id yra unikalus tai patogiausias selektorius.
WebElement username = driver.findElement(By.idopen in new window("userName"));
prastasnameIeškoti elementų pagal tekstą aprašytą prie name atributo.
WebElement username = driver.findElement(By.nameopen in new window("email"));
prastaslink textIeško įvestos frazės "tik matomų nuorodų" pagal jose esantį tekstą.
WebElement vardas = driver.findElement(By.linkText("Marius"));
prastaspartial link textIeško pagal nurodytą tekstą "tik matomų nuorodų" ir radūs pateikia pirmą atitikusį elementą.
WebElement here = driver.findElement(By.partialLinkText("here"));
prastastag nameIeškoti elementų pagal nurodytą žymės (angl. tag) vardą. pvz. div, p, h1, td, tr, footer ir t.t.
WebElement username = driver.findElement(By.tagName("tr"));
vidutinisxpathIeškoti elementų pagal xpath išraišką plačiau rasite čiaopen in new window.
WebElement bookName = driver.findElement(By.xpath("/bookstore/book[price>35.00]/title"));

Tips on using selectors

In general, if HTML IDs are available, unique, and consistently predictable, they are the preferred method for locating an element on a page. They tend to work very quickly, and forego much processing that comes with complicated DOM traversals.

If unique IDs are unavailable, a well-written CSS selector is the preferred method of locating an element. XPath works as well as CSS selectors, but the syntax is complicated and frequently difficult to debug. Though XPath selectors are very flexible, they are typically not performance tested by browser vendors and tend to be quite slow.

Nuoroda darbui su Selenium 4 "Relative locators"

Plačiau apie Selenium Relative locators rasite čia anglų kalba: https://www.selenium.dev/documentation/webdriver/locating_elements/open in new window

Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.15.8