Guidance on invoking a function/method from one class to another in Selenium WebDriver using Java

I need to invoke the login and search methods from two distinct classes into the main class. I have created separate classes for login and search, but when I try to call both methods in the class that contains the main method, I encounter the following error>>

Exception in thread "main" java.lang.NullPointerException at Test_package_Ted_baker.search.search_1(search.java:14) at Test_package_Ted_baker.SHopping.main(SHopping.java:16)

Could someone assist me in finding a solution on how to call methods from one class to another class? Below is my code>>

Main class

package Test_package_Ted_baker;

import org.openqa.selenium.WebDriver;

public class SHopping {
static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        // Main method code here
    }
}

Login class

package Test_package_Ted_baker;
// Login class code here

Searching class

package Test_package_Ted_baker;
// Searching class code here

Answer №1

It may come across as blunt, but the first step is to familiarize yourself with

  • Null Pointer Exception by checking out this resource.
  • Tips for debugging using a StackTrace
    from this source.
  • The drawbacks of using absolute xpaths or xpath locators in general
    found here.

From the error message you've received,

Exception in thread "main" java.lang.NullPointerException at Test_package_Ted_baker.search.search_1(search.java:14) at Test_package_Ted_baker.SHopping.main(SHopping.java:16)

it's evident that there is an issue with your code in the Search class

WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));

an incorrect locator is being used here. I suggest trying to identify the element using its id attribute, like so

 driver.findElement(By.id("search")); 

or if xpaths are preferred, then

 driver.findElement(By.xpath("//*[@id="search"]"));

this should resolve the NPE - however, relying heavily on absolute xpaths is discouraged.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When you click on the bottom of the list, you access the element at the top of the list

Currently, for my test using Java and Selenium, I need to click on an element at the bottom of a list within the application. The code snippet I am using is as follows: wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()=&apos ...

Hover over the object to uncover and select the concealed element

In the C# environment with Selenium Webdriver, I am faced with the challenge of hovering over an element to reveal a menu containing hidden elements. From this menu, I need to click on an element that was not previously visible. It is important not to clic ...

Unable to use drag and drop feature on Unlayer website

I have tried using drag and drop on the Unlayer website with various combinations, but unfortunately it does not seem to be working. Below is the code we attempted to use with Selenium 4: @Test() public void test() throws InterruptedException { ...

Instructions for choosing an item from a dropdown list by searching for partial text with Selenium and Python

When utilizing the Selenium Kit in Python, I am facing a challenge in selecting an option from a drop-down menu. To accomplish this, I am using python driver.select_by_visible_text(). However, my issue lies in the fact that the visible text always include ...

Unable to establish a connection with Geckodriver

Attempting to execute the Selenium example script with Ruby on Rails, I encountered an issue when trying to run it with a proxy. Below is the code snippet: require 'rubygems' require 'bundler/setup' # require your gems as usual requir ...

Executing Katalon Tests

I currently have a test case set up in Katalon for a specific action. However, I have a Browser already open and logged into the website where I need to run the test case. Is there a way to utilize that existing window and continue executing the test cas ...

Differences between Regular Expressions in VBScript and Java

During the feasibility analysis of a GUI application, we are currently in the process of comparing QTP (VBScript) and Selenium (Java) tools. Selenium's advantage lies in being an open source tool which makes it cost-effective. The GUI application we a ...

What is the process for extracting HTML content using the JavaScript executor?

import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; public class WebDriverExample { public static void main(String[] args) { System.setProperty("webdriver.c ...

Guide on setting an attribute value with JavaScriptExecutor in Selenium WebDriver

I am attempting to set an attribute value for all instances of the same type of <img> tag on My website, for example: <img src="images/temp/advertisement.png"> and I want to set style="display: none" so that I can hide them. I have tried the ...

Discovering All Cookies on the Current Page with Selenium Webdriver

Seeking assistance on how to retrieve all cookies from the current page using Chrome driver with Java. I need help in retrieving all cookies, closing the browser, and then opening a new browser with the old cookies intact. ...

Selenium Web-Driver Pop-up

I'm encountering an issue with a popup message when trying to write using Selenium. Would appreciate any assistance or insight. Here is my code:- public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "F:\\ge ...

Encountering a "connect_until_stable" error while attempting to run Selenium on a Heroku Ruby on Rails deployment

I am facing an issue while trying to execute a file with selenium on Heroku. The error message I encounter is as follows: /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/service_manager.rb:130:in `connect_until_sta ...

What is the best way to access the link within a TR element using XPATH?

Selecting a target from the menu in this HTML code is required, with one of the targets labeled as ANY TARGET to proceed with the process. <tr id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0" class=" iceRowSelMouseOver" tabindex="0" onmouseover= ...

Verifying a tooltip value for a specific section (bar) on a bar chart using Selenium WebDriver

Is there a way to extract the value 31732 from the margin (sum) tooltip for Electrolux vendor in the bar chart shown below? https://i.stack.imgur.com/CIgIW.png The following HTML provides the code for the div enclosing this value. Your advice on how to ...

Looking to extract an Instagram post caption using Java Selenium?

My code is able to print hashtags from a post, but it doesn't seem to be printing the caption. The error message indicates that the caption should be treated as an element. driver.findElement(By.xpath("//*[@id=\"react-root\"]/section/main/a ...

Choosing specific elements based on criteria with selenium in Python

Is it possible to use Selenium to extract elements that match specific criteria? Specifically, I am trying to create a script that will identify all Twitch channels with more than X number of viewers. By inspecting the element, I found this: <p class= ...

Hidden Time Lag Using Selenium

Currently, I am honing my skills with selenium. Despite initially attempting page scraping using beautifulsoup, I made the decision to switch to selenium due to the slower loading time of the page. Consequently, I integrated a delay into my code, experimen ...

Gathering data from websites using pandas, beautifulsoup, or selenium

As a beginner in the world of Python, I am currently diving into the realms of tensorflow and (py)torch. My goal is to use these tools for lottery predictions, despite common beliefs that it is impossible. Can someone assist me in scraping data from the we ...

Exploring the wonders of incorporating clipboard functionality into web automation with Selenium and Py

I am encountering an issue with my selenium script that operates successfully on Windows without a virtual display, but fails on Linux where I need to use a virtual display with pyvirtualdisplay or xvfbwrapper to click a copy to clipboard button. The probl ...

Error message appeared after clicking the button due to timeout

When I click the button, the page refreshes and redirects to another page. However, in my case, after clicking the button I set a timeout for the page refresh, but it shows a timeout exception without waiting for the specified time. I have tried using diff ...