In order to tell our selenium code what and where to enter password and username, to do that we need to find exactly were to find the will be doing something similar intended to automate this step to avoid this login every time we try running the test case.
In order to tell our selenium code what and where to enter password and username, to do that we need to find exactly were to find the
name
id
class
classname
You might be wondering how to get their argument, we have a way to get to that by visiting the website you wish to get argument from.
Then use F12 to open the developer option in your browser. Click+Shit+C to launch element inspector, click on the input field then you will find anyone of those element type we can use to detect it.
Code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Locator {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\Software\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://github.com/login");
driver.findElement(By.name("login")).sendKeys("prathapdom@gmail.com");
driver.findElement(By.name("password")).sendKeys("thisisnotmypassword");
driver.findElement(By.name("commit")).click();
}
}
Explanation
Use the the above technique to view the argument and the value of the above mentioned locator of the webpage you visited.
driver.findElement(By.name("login")).sendKeys("prathapdom@gmail.com");
Then enter the value you find for the argument name using Browser inspector into the By.locator method.
Similarly do the same for other locator, Many sites does not support or use all these locator so you might be able to use anyone of the locators.