Monday, October 17, 2016

Selenium 101.

Basics
  • driver.findElements(...).findElement(...)

By ID
  • Java: driver.findElement(By.id(...)) 
  • Python: driver.find_element_by_id(...)
WebElement id1 = driver.findElement(By.id("id1"));

// Can combine with Xpath searching
id1.findElement(By.xpath("/p"));

By Name
  • Java: driver.findElement(By.name(...))
  • Python: driver.find_element_by_name(...)
@Test
public void findElementByName() {
        WebElement input =
           driver.findElement(By.name("keyword"));
        assertEquals("Type in your keyword",
           input.getAttribute("value"));
}
By ClassName
  • Java: driver.findElement(By.className(...))
  • Python: driver.find_element_by_class_name(...)
@Test
public void findElementByClassName() {
        WebElement label =
           driver.findElement(By.className("label"));
        assertEquals("Search", label.getText());
}
By TagName
  • Java: driver.findElement(By.tagName(...))
  • Python: driver.find_element_by_tag_name(...)
@Test
public void findLisInsideUl() {
        WebElement ul =
           driver.findElement(By.id("list2"));
        List lis =
           ul.findElements(By.tagName("li"));
        assertEquals(3, lis.size());
        List lis2 =
           driver.findElements(By.tagName("li"));
        assertEquals(8, lis2.size());
}
By Links
  • Java: driver.findElement(By.linkText(...))
  • Python: driver.find_element_by_link_text(...)
  • Java: driver.findElement(By.partialLinkText(...))
  • Python: driver.find_element_by_partial_link_text(...)
@Test
public void testFindLinkByText() {
        WebElement link =
           driver.findElement(By.linkText("Facebook"));
        String url = link.getAttribute("href");
        assertEquals("https://www.facebook.com/", url);
}

@Test
public void testFindLinkByPartialText() {
        WebElement link =
           driver.findElement(By.partialLinkText("Digg"));
        String url = link.getAttribute("href");
        assertEquals("http://www.digg.com/", url);
}
By XPATH
  • Java: driver.findElement(By.xpath("nodename..."))
  • Python: driver.find_element_by_xpath("nodename...")
  • nodename:
    • /     - root element
    • //    - current element/node
    • .     - current node
    • ..    - parent of current node
    • @  - an attribute
    • [ ]  - predicate
By.xpath(“//form[@name=‘site-search']// input[@type='submit']")
By.xpath(“//form[@name=‘site-search']/ input[@type='submit']")

/****************************
* useful xpath expressions
*****************************/
//input[@id], //img[@alt], //img[not(@alt)]
//input[@id=‘username’]
//input[@type=‘submit’ and @value=‘login’]
//input[@type=‘submit’][@value=‘login’]
//input[@type=‘submit’ or @value=‘login’]
//input[starts-with(@name, ‘username’)]
//input[contains(@id, ‘name’)]
//input[ends-with(@id, ‘name’)]

WebElement p2 = id1.findElement(By.xpath("./p"));
assertEquals("World", p2.getText());
WebElement p3 = id1.findElement(By.xpath("//p"));
assertEquals("Hello", p3.getText());
WebElement p4 = id1.findElement(By.xpath(".//p"));
assertEquals("World", p4.getText());

@Test
public void findUl() {
        WebElement list =
           driver.findElement(By.xpath("//ul"));
        assertEquals("list1", list.getAttribute("id"));
}

@Test
public void findLis() {
        List lis =
           driver.findElements(By.xpath("//li"));
        assertEquals(8, lis.size());
        List lis2 =
           driver.findElements(By.xpath("//ul/li"));
        assertEquals(7, lis2.size());
        List lis3 =
           driver.findElements(By.xpath("//ul//li"));
        assertEquals(8, lis3.size());
}

@Test
public void findLinksWithTargetAttribute() {
        List links =
           driver.findElements(By.xpath("//*[@target]"));
        assertEquals(2, links.size());

        String[] expectedUrls =
           new String[] {
                               "https://www.google.com/",
                               "http://www.msn.com/"
           };
        List actualUrls = new ArrayList();

        for (WebElement link : links) {
           actualUrls.add(link.getAttribute("href"));
        }

        assertArrayEquals(expectedUrls,
           actualUrls.toArray());
}

@Test
public void findLinkThatOpenNewTab() {
        WebElement link =
           driver.findElement(By.xpath("//a[@target='_blank']"));
        assertEquals("MSN Link", link.getText());
}

@Test
public void findAllSecureLinks() {
        List links =
           driver.findElements(
              By.xpath("//ul[@id='list2']//a[starts-with(@href, 'https')]")
                );
        System.out.println("Secure links:");
        for (WebElement link : links) {
           System.out.println(link.getAttribute("href"));
        }
}

@Test
public void findAllPDFLinks() {
        List links =
           driver.findElements(
              By.xpath("//ul[@id='list2']//a[ends-with(@href, 'pdf')]")
                );
        System.out.println("PDF links:");
        for (WebElement link : links) {
           System.out.println(link.getAttribute("href"));
        }
}

CSS
  • Java: driver.findElement(By.cssSelector(...))
  • Python: driver.find_element_by_css_selector(...)
  • Tag selector         : By.cssSelector("input")
  • Class selector      : By.cssSelector(".login")
  • ID selector           : By.cssSelector("#title")
  • Attribute selector (can be full or partial match)
    • a[href^="https"]       - starts with
    • a[href$=".pdf"]        - ends with
    • a[href*="google"]    - contains

Example
  • Find child elements from parent elements
    • By.cssSelector("tbody > tr:nth-child(3)”)
    • form > label:last-child vs. label:last-of-type
  • Find sibling elements
    • form > input[name^='username'] + * + label
  • Find elements based on various states
    • :enabled, :disabled, :checked, :focus, :hover, :active
    • By.cssSelector(“input[type='checkbox']:not(:checked)")
  • CSS selectors

WebElement ele1 = driver.findElement(By.cssSelector(".primary-btn"));
WebElement ele2 = driver.findElement(By.cssSelector("btn.primary-btn"));
WebElement ele3 = driver.findElement(By.cssSelector("btn.primary-btn"));

WebElement Email = driver.findElement(By.cssSelector("input[id=email]"));
Email.SendKeys("hello@sampleemail.com");


Interactive Elements

No comments:

Post a Comment