Error regarding variable in Selenium TestNG

how to declare variable in selenium webdriver
how to store value in variable in selenium webdriver
parameterization in selenium webdriver using java
how to pass parameters in selenium webdriver
how to pass multiple parameters in testng
parameterization in selenium webdriver using excel
selenium testng framework setup
how to declare webdriver globally in selenium

I'm trying to run below code from IntelliJ but in turns an error (below). I just want to click a button on the website using a Xpath locator and adding an assertion to validate my test. What is the best approach to build such a simple test?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.util.List;

public class ButtonTest extends CommonScenario {

  private static WebDriver driver;

  @Test()
  public void button_test() {
    button b = driver.findElements(By.xpath("//button[text()='Teleworking"));
  }

Error:

Error:(16, 5) java: cannot find symbol
      symbol:   class button
      location: class selenium.ButtonTest

There is no button class. driver.findElements returns a list of WebElement

List<WebElement> elements = driver.findElements();

If you want a single element use driver.findElement

WebElement element = driver.findElement();

Set up Constant Variables in Selenium Automation Framework , Step by step process to Set up Constant Variables in Selenium Automation framework.Set up TestNG, LOg4j, Data Driven, POM, Functional Libs and Modularity. 2) Create a 'New Class' file, by right click on the above-created Package and  Parameters in TestNG is similar to annotations in TestNG in their declaration. Similar to parameters in any other programming language , they are declared to pass some values onto the function. A simple reason to use parameters is that they let us run a function many times with different values or to run different functions with the same values.

There is no class called button in order to initiate the object b with. Try WebElements. So the method's code would be

public void button_test() {
List<WebElement> b = driver.findElements(By.xpath("//button[text()='Teleworking"));
}

And note that you are using findElements and not findElement which would return a list of webelements and not a single webelement.

Selenium: Getting error with webElement or Webdriver "Drive , you are declaring another variable for driver (like WebDriver driver=null; ). So a new variable driver is created (and this variable is not the  Recent in Selenium. How to Handle alerts like Irctc alert in selenium? 5 days ago; Not able to find the element in a new form in the same webpage ,which popup after clicking a link 5 days ago; How to write/insert (Embed) the image object into the excel in Selenium WebDriver (POI Jar ) using Java Programming language? Jun 24

Please check if you have single element with the xpath "//button[text()='Teleworking". If so then update your script like below

 WebElement button = driver.findElement(By.xpath("//button[text()='Teleworking"));

If you have more than one button with the same xpath as above then you need to handle it through List and in that case you can use below XPath.

List<WebElement> button = driver.findElements(By.xpath("//button[text()='Teleworking"));

Selenium Testng, getting error Null Pointer exception, Although there is a class-scope variable driver , the Login() method stores the created WebDriver object in a method-scope variable. why it through me nullpointer exception after removing duplicate element it worked for me. For example, in this Selenium Java tutorial regarding Selenium login with Java, a browser, by default, will be in minimized mode. However, we can set up the browser in the maximize mode. Below is

Selenium TestNG Interview Questions and Answers Part2, Explore 20 new Selenium TestNG interview questions and answers for 2017. What is the best approach for reading a JavaScript variable from Selenium WebDriver? How will you get How to fix the below chrome driver error in Selenium? TestNG-Eclipse Setup. Step 1 − Launch Eclipse and create a 'New Java Project' as shown below.. Step 2 − Enter the project name and click 'Next'.. Step 3 − Navigate to "Libraries" Tab and Add the Selenium Remote Control Server JAR file by clicking on "Add External JAR's" as shown below.

Parameterization in TestNG using testng.xml, Some times it may be required for us to pass values to test methods during run We will get an error if we dont specify the parameter in testng.xml file to a test  Selenium Framework Creation Strategy Using a Sample Project to Access Test Data from Excel. In this Selenium automation framework tutorial we will learn creating framework and Accessing test data from the excels.

Store Variables, Echo, Alert, PopUp handling in Selenium IDE, In this tutorial, we will learn, Store commands, Echo commands, Alerts and Popup handling, Confirmations and about Multiple Windows. In code, we have used "log" as a reference variable referencing getLogger method of Logger Class Logger log = Logger.getLogger("devpinoyLogger"); Use "log" referencing variable and debug method to log the information we want. log.debug("--information--"); What is a LogExpert tool? LogExpert tool is a tool for Windows developed to tail the logs

Comments
  • Ok, I want to click that particular element (button) using below code. What's the solution for this?: @Test() public void button_test() { driver.findElement(By.xpath("//button[text()='Teleworking")).click(); }
  • @Fahri what is stopping you from doing it?
  • I forgot to mention sorry. I get following error: java.lang.NullPointerException at selenium.ButtonTest.button_test(ButtonTest.java:16) <18 internall calls>