Tell me why in one situation the IDE executes the click () method and swears in the other? In this code, it works as it should.

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SecondTestCase { public static void main(String[] args) throws InterruptedException { // Create a new instance of the Firefox driver WebDriver driver = new FirefoxDriver(); //Launch the Website driver.navigate().to("https://www.seleniumhq.org/docs/"); System.out.println("Successfully opened the website www.seleniumhq.org/docs/"); String a = driver.getTitle(); System.out.println("Title is:"+a); //Строка в которой используется метод click() driver.findElement(By.xpath("//div[@class='toctree-wrapper compound']/ul/li[1]/a/em")).click(); String b = driver.getCurrentUrl(); System.out.println("New page is:"+b); driver.navigate().back(); String c=driver.getCurrentUrl(); System.out.println("New new back url:"+c); // Close the driver driver.close(); } 

}

But if at the end of the line that follows the commentary (// Line in which the click () method is used), actually remove the click () method and transfer it to the next one as driver.click (); then we get the error "Cannot resolve method". Example:

 //Строка в которой используется метод click() driver.findElement(By.xpath("//div[@class='toctree-wrapper compound']/ul/li[1]/a/em")); driver.click(); 
  • Because the click method is in what the findElement method findElement and not in the driver object? - andy.37

1 answer 1

driver is a WebDriver interface object that does not contain a click () method.

You click () call after findElement, which returns a WebElement object that contains this method.

The following code will be correct

 WebElement element = driver.findElement(By.xpath("//div[@class='toctree-wrapper compound']/ul/li[1]/a/em")); element.click(); 

In short, WebDriver is an interface for working with browsers. Go somewhere, find something, manipulate browser logs.

WebElement - to work with objects on the page: click, scroll, getText ()