Skip to content

Instantly share code, notes, and snippets.

@angiejones
Created July 14, 2019 21:07
Show Gist options
  • Save angiejones/eacee133bdcf00f155343de3061091bd to your computer and use it in GitHub Desktop.
Save angiejones/eacee133bdcf00f155343de3061091bd to your computer and use it in GitHub Desktop.
Answer Key for Chapter 3 of Selenium WebDriver Java Course on Test Automation University
package base;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class BaseTests {
private WebDriver driver;
public void setUp(){
System.setProperty("webdriver.chrome.driver", "resources/chromedriver");
driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.findElement(By.linkText("Shifting Content")).click();
driver.findElement(By.linkText(("Example 1: Menu Element"))).click();
List<WebElement> menuItems = driver.findElements(By.tagName("li"));
System.out.println("Number of menu items: " + menuItems.size());
driver.quit();
}
public static void main(String args[]){
BaseTests test = new BaseTests();
test.setUp();
}
}
@venkatams
Copy link

Hi Angie,
I've used the following command in line #20:
driver.findElement(By.partialLinkText("Example 1")).click();

This has worked just as well for me.
Thanks

@lakshmiviln
Copy link

just went one step ahead and printed all the menu items as well, with following code

List menuElements = driver.findElements(By.tagName("li"));
for( WebElement menu : menuElements){
System.out.println(menu.getText());

@mohammedyouness
Copy link

mohammedyouness commented Apr 23, 2020

I think using CSS selectors is cleaner than using LinkText as it's more error-prone If I misspelled the link text or just forgot any spaces between the words
driver.findElement(By.cssSelector("a[href='/shifting_content']")).click();
driver.findElement(By.cssSelector("a[href='/shifting_content/menu']")).click();
List menuItems = driver.findElements(By.tagName("li"));
System.out.println(menuItems.size());

@sanjia022
Copy link

Hi Angie,

I used CSS and Xpath selector. Here is the code:

            WebElement shifting = driver.findElement(By.xpath("//div[@id='content']/ul//a[@href='/shifting_content']"));
	shifting.click();
	WebElement menuItems = driver.findElement(By.cssSelector("div#content > div > a:nth-of-type(1)"));
	menuItems.click();
	List<WebElement> lists = driver.findElements(By.tagName("li"));
	System.out.println(lists.size());

@helenayele
Copy link

Thanks Angie,
Thanks for the group members in this course.
My code looks like this:
WebElement shifitingContentLink = driver.findElement(By.linkText("Shifting Content"));
shifitingContentLink.click();
WebElement exampleOne = driver.findElement(By.partialLinkText("Menu Element"));
exampleOne.click();
List lists = driver.findElements(By.tagName("li"));
System.out.println(lists.size());

And working fine

@Dksoni81291
Copy link

I've used below code- combination of CSS selector and Xpath.

    driver.get("https://the-internet.herokuapp.com/");
    driver.manage().window().fullscreen();

    WebElement Inputlink = driver.findElement(By.linkText("Shifting Content"));
    Inputlink.click();

    WebElement Option = driver.findElement(By.cssSelector("a[href='/shifting_content/menu']"));
    Option.click();

    List<WebElement> Weblinks =  driver.findElements(By.xpath("//ul/li/a"));
    System.out.println(Weblinks.size());
    
    for(WebElement menu: Weblinks){
        System.out.println(menu.getText());
    }

@dutchviewjyothi
Copy link

Hey Angie, My code for the above scenario looks like this:

System.setProperty("webdriver.chrome.driver","C:\chrome\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().maximize(); // to maximize the window
System.out.println(driver.getTitle());
WebElement Shift = driver.findElement(By.linkText("Shifting Content"));
Shift.click();
WebElement Menu = driver.findElement(By.linkText("Example 1: Menu Element"));
Menu.click();
List lists = driver.findElements(By.tagName("a"));
System.out.println(lists.size());

Thank you

@ayagharieb
Copy link

ayagharieb commented Aug 31, 2021

Hello angi,
thanks alot for your easy and great course,
i'm very enthusiastic while studying it ,
Here you are "my code" but i add from your solution just this sentence ["Number of menu items: " + ] to my code :

package base;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class BaseTests {
private WebDriver driver ;
public void setup()
{
System.setProperty("webdriver.chrome.driver","resources\chromedriver.exe");
driver= new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");

    WebElement inputslink = driver.findElement(By.xpath("//*[text()='Shifting Content']"));
    inputslink.click();
    WebElement MenuElement = driver.findElement(By.xpath("//a[text()='Example 1: Menu Element']"));
    MenuElement.click();
    List<WebElement> CountMenuElement = driver.findElements(By.xpath("//div[@class='example']/ul/li"));
    System.out.println("Number of menu items: " +CountMenuElement.size());
    System.out.println(driver.getTitle());
}
public static void main (String args [] )
{
    BaseTests test = new BaseTests();
    test.setup();
}

}

@samiullah
Copy link

`package TESTIM;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class ShiftingContent {

WebDriver driver;


@BeforeClass
public void setup() {
	
	System.setProperty("webdriver.chrome.driver","/Users/sami/eclipse-workspace/Testa/chromedriver 3");
		 
	driver = new ChromeDriver();
	driver.manage().window().maximize();
			
}


@Test
public void OpenBrowser() {
	driver.get("https://the-internet.herokuapp.com/");
}


@Test
public void Sigin() throws InterruptedException {
	WebElement input = driver.findElement(By.linkText("Shifting Content"));
	input.click();
	Thread.sleep(3000);
	
	WebElement example = driver.findElement(By.linkText("Example 1: Menu Element"));
	example.click();
	
	Thread.sleep(3000);
	
	List<WebElement> menus = driver.findElements(By.tagName("li"));
	System.out.println(menus.size());
	

}

@afterclass
public void closeBrowser() {
driver.quit();
System.out.println("Quit browser");

}

}
`

@qaolgawu
Copy link

Hello Angie,

please find my code below for Ch3. Selenium WebDriver Java Course on Test Automation University. Thank you!

package base;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

import static java.lang.System.out;

public class BaseTests {

private WebDriver driver;

public void setUp() {
    System.setProperty("webdriver.chrome.driver", "resources/chromedriver");
    driver = new ChromeDriver();
    driver.get("https://the-internet.herokuapp.com/");

    WebElement inputsLink = driver.findElement(By.linkText("Shifting Content"));
    inputsLink.click();

    WebElement inputsLink2 = driver.findElement(By.linkText("Example 1: Menu Element"));
    inputsLink2.click();

    List<WebElement> menuItems = driver.findElements(By.tagName("li"));
    System.out.println("Number of menu items: " + menuItems.size());

    driver.quit();
}

public static void main(String args[]) {
    BaseTests test = new BaseTests();
    test.setUp();
}

}

@luizeraberaldo
Copy link

Hi guys! I've used below solution with xpath and contains to validate the texts inside the xpath

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseTest {

private WebDriver driver;

public void setUp() {
	System.setProperty("webdriver.chrome.driver", "C:\\\\seleniumDrivers\\\\chrome\\\\chromedriver_111.exe");
	driver = new ChromeDriver();
	driver.get("https://the-internet.herokuapp.com/");
	
	
	driver.manage().window().maximize();
	System.out.println(driver.getTitle());
	
	
	WebElement shift = driver.findElement(By.xpath("//a[contains(text(),'Shifting Content')]"));
	shift.click();
	
	WebElement example1 = driver.findElement(By.xpath("//a[contains(text(),'Example 1: Menu Element')]"));
	example1.click();
	
	List <WebElement> var1 = driver.findElements(By.tagName("li"));
	System.out.println(var1.size());

	}

public static void main(String[] args) {
	BaseTest test = new BaseTest();
	test.setUp();
}

}

@heshamafify
Copy link

I have used the following command by accessing the web page of [Shifting Content] directly
package base;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class BaseTests {
private WebDriver driver;
public void setup(){
System.setProperty("webdriver.chrome.driver","resources2/chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/shifting_content/menu");

   List<WebElement> links=driver.findElements(By.tagName("li"));
   System.out.println(links.size());
    WebElement exitlink= driver.findElement(By.linkText("Exit Intent"));
    exitlink.click();

    System.out.println(driver.getTitle());

}
public static void main(String args[]){
    BaseTests test= new BaseTests();
    test.setup();

}

}

@MashigoJohn
Copy link

Hi guys i wrote my code well but i dont undestand how and why do we use the By.tagName("li) the li key why are we using it??

@Morena-s
Copy link

@MashigoJohn This is because we want to get all the lists on the page, this li (List) has the hyperlink tags inside, but if we get the hyperlinks, this will get all the hyperlinks tags on the page and we dont want to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment