• +43 660 1453541
  • contact@germaniumhq.com

Simple Germanium Selectors


Simple Germanium Selectors

Writing selectors shouldn’t be always complicated. Here is a quick comparison between a few more popular selectors.

1. Find an element inside another element

For elements without text:

Selenium

wd.find_element_by_css('.parent-class .child-class')

Germanium

Css('.parent-class .child-class').element()

In case there is text in the element, the selector in case of Selenium must be now XPath. Fortunately in Germanium this is much easier:

Selenium

wd.find_element_by_xpath(
    "//div[contains(@class, 'parent-class')]" +
    "/div[contains(@class, 'child-class')]" +
        "[string() == 'child text']")

Germanium

Element("div",
        css_classes="child-class",
        exact_text='child text')
    .inside(Element("div",
                    css_classes="parent-class"))

2. Finding an element outside an element

In case the element must not be present in its parent, the XPath expression becomes far more complicated, as if it wasn’t already:

Selenium

wd.find_element_by_xpath(
   "//div[contains(@class, 'child-class')]" +
        "[string() == 'child text']" +
        "[not(ancestor::div[@class, 'parent-class'])]")

Germanium

In Germanium we just replace the inside with outside.

Element("div",
        css_classes="child-class",
        exact_text='child text')
    .outside(Element("div",
                     css_classes="parent-class"))

Conclusion

Using inside and outside from the Germanium selector API greatly simplifies writing readable and easily to maintain code, as a great alternative to XPath complexity.