Understanding ByIdOrName

The way ByIdOrName works is that the ID takes precedence. If a particular element is not found with the ID that is specified, the search is made based on a name. If the element is not found after the second search, it throws a NoSuchElementException.

Let's have a look at the code to handle this.

The following code uses an implicit wait of 30 seconds for each element on the page. First, it searches based on the ID and then on the name. Notice that we have to invoke the constructor in order to use this locator:

public class ByIdOrNameDemo {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\src\main\resources\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("C:\Users\Bhagyashree\Desktop\Documents\myHTML.html");
driver.findElement(new ByIdOrName("fid")).sendKeys("FirstTest");
driver.findElement(new ByIdOrName("lname")).sendKeys("LastTest");
}
}

Next we will have a look at ByAll.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.251.155