Feeling trapped by the NullPointer Exception bug

I am currently automating the add to cart process on the website "http://www.oyehappy.com" using TestNG POM Structure. I have encountered a NullPointer Exception while handling autosuggestion. The code for my POM Class is as follows:

public class productPage_POM {
public WebDriver driver;

@FindBy(id="tagsf2")
private WebElement city_txtbox;
public productPage_POM(WebDriver driver)
{
    PageFactory.initElements(driver, this);
}

public void sel_adr()
{
    city_txtbox.sendKeys("gan");     // facing nullpointer exception here

    String xp = "//a[starts-with(text(),'gan')]";
    List<WebElement> allList = driver.findElements(By.xpath(xp));
    int count = allList.size();
    System.out.println(count);
    for (int i = 0; i < count ; i++) 
    {
        String name = allList.get(i).getText();
        System.out.println(name);
        if(name.contains("Gandhinagar"))
        {
            allList.get(i).click();
            break;
        }
    }

Below is the HTML structure:

<div class="pro_detail">
<div id="changeImage" class="pro_detail_image">
<div class="pro_detail_title">
<div class="pro_detail_size">
<div class="pro_detail_form">
<input id="cityIdHidden" type="hidden" value="CITY-1219">
<input id="hasShippingOption" type="hidden" value="">
<input id="cityIdHidden1" type="hidden" value="">
<input id="liCount" type="hidden" value="1">
<div id="hiddenField">
<div class="pro_detail_form_inn">
<label>1. Where do you want to deliver this?</label>
<input id="tagsf2" class="inp_pro_detail2 election ui-autocomplete-input" type="text" value="Enter City" onchange="showAddtocart();" onblur="WatermarkOnblur('Enter City','tagsf2');shippingOption();" onfocus="WatermarkOnfocus('Enter City','tagsf2')" autocomplete="off">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">CITY-1219</span>
<div class="clear"></div>
<div id="cityNotAvailable" class="error_message hide" style="display: none;">
</div>
<div class="textbox">
</div>
<div class="pro_detail_form">
<div id="divDeliveryText" class="pro_detail_form" style="display:none;">

Answer №1

Perhaps you are viewing the Angular website, where the id changes dynamically. This could be why your code is unable to locate the defined id. Consider using a different selector that does not change dynamically. (I'm not very familiar with JavaScript).

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

Using Typescript to create a Checkbox Grid that displays pipe-delimited values and implements a custom validation rule

I am currently working with a checkbox grid that contains pairs of AccountIds (consisting of x number of digits) and file names separated by a pipe delimiter. The file names are structured to always begin with either PRC or FE followed by a varying combin ...

How do I assign a default value to an optional parameter in a derived class in Typescript?

One of my classes is called ClientBase: export class ClientBase { constructor(private uri: string, private httpClient: HttpClient) { } // Contains Various Methods } I have multiple subclasses that are derived from the ClientBase For instance: @I ...

Adjusting the quantity of buttons in real-time following an ajax request

Creating buttons dynamically in an Ajax success function can be a challenge when the number of buttons varies each time. I am able to create the buttons, but since the exact number is unknown, adding the correct number of button listeners becomes tricky. ...

Encountering an issue when passing a response using coverage.js

I am utilizing coverage.js to showcase data. When I pass my variable containing the coverage response into an HTML file, similar to how it's done in Angular to display expressions, I encounter a syntax error: <div class="container" style="margin- ...

How to extract data from Facebook's JSON using Javascript

I am utilizing a JavaScript API to extract Facebook comments. After receiving the JSON result below, I'm wondering how I can unpack and utilize them on my webpage? { "id": "1234567891_2823098717038_3160191", "from": { "name": "U ...

MUI: Issue with pseudo element appearing cropped outside of Paper container

I am facing an issue where a red arrow pseudo element '::before' is partially cut off outside its container '.MuiPaper-root'. I need the arrow to remain visible, any suggestions on how to fix this? Here is the relevant code snippet and ...

How to assign a click event to dynamically generated HTML elements using jQuery

My goal is to attach an onclick event to a dynamically inserted element using jQuery However, I am facing an issue where the function does not execute. I would appreciate it if someone could help me determine why this example is not functioning correctly ...

The event listener for browser.menus.onClicked is dysfunctional in Firefox

Currently, I am in the process of developing my own Firefox extension and I have encountered an issue with adding a listener to an onclick event for a context menu item. manifest.json { "manifest_version": 2, "name": "My exten ...

How can I integrate the jQuery Plugin Mapael with Angular 5?

While exploring various methods that tackled the issue of integrating jQuery Plugins, I decided to start with the fundamentals. To begin with, I installed the jQuery plugin: npm i jquery Next, I included the TypeScript definition: npm install -d @types ...

A single click causes the entire row of cards to extend seamlessly

The cards on this tab were generated using a loop and the data is sourced from a database. When I click on the "Show More" button, the entire visible column expands along with it. The expansion does not reveal the content immediately; instead, the content ...

execute selenium using crontab (python)

In my project, I have a Python script that utilizes Selenium to call Chrome. The specific line of code is as follows: ff = webdriver.Chrome('/home/user01/webScraping/CollectAndGo/chromedriver') This Python script is executed from a shell scrip ...

Leveraging both onmouseover and onmouseout for container expansion

My goal is to utilize JavaScript along with the HTML events "onmouseover" and "onmouseout" to create a dynamic container. Essentially, I want the container to act as simply a heading when the mouse is not hovering over it, but expand to display additional ...

Obtain the present location of the cursor within the TinyMCE editor

Despite various attempts, I have struggled to determine the current cursor position inside TinyMCE. My goal is to implement a change control feature that captures the starting point of text entry within the TinyMCE "textarea," allowing me to save the ente ...

Setting the memory limit for OOM Killer specifically for the Chrome browser

Chrome is showing an error message: gfp_mask=0xd0, order=0, oom_score_adj=300 During my testing with the headless Chrome browser and Selenium, I encountered the above issue. ...

Tips to locate elements with selenium in Python by utilizing ids and classes within divs

Can anyone help me with finding an element using both id and class in a div using Selenium in Python, and then clicking on it? <div id="abc" class="xyz" style="" role="presentation"></div> This is the code I am currently using: arrow = drive ...

Streaming videos using Flask with a custom template

After successfully implementing the DWA path planning algorithm in C with Python bindings, I have decided to create a web application demo. The concept involves a "robot" following the mouse using DWA, while allowing users to draw walls for objects. My ini ...

Retrieve and save only the outcome of a promise returned by an asynchronous function

I am currently working on an encryption function and have encountered an issue where the result is returned as an object called Promise with attributes like PromiseState and PromiseResult. I would like to simply extract the value from PromiseResult and s ...

Trouble arises when trying to open a new window using the Angular 8 CDK

I am attempting to open my component in a new window, similar to this example: https://stackblitz.com/edit/angular-open-window However, when the window opens, my component is not displayed and I receive the following error in the console: Error: Must pro ...

The functions Show() and Hide() may not work in all scenarios within jQuery

I'm currently developing a website that allows users to participate in quizzes. Each quiz consists of 20 questions divided into three sections: 1 mark for 10 questions, 2 marks for 5 questions, and 4 marks for 5 questions. For each question, there are ...

Error: Unable to access property of an undefined value (ExpressJS/POST)

I have gone through all the similar questions but none of the solutions are working for me. I am facing an issue with my node js app where I am unable to print the input text from a form while using body-parser. Here is my index.ejs file: <fo ...