Managing click events in Javascript with Selenium Webdriver

Currently utilizing Selenium Webdriver for my testing. The test-case involves:

  1. Logging in to the site.
  2. Clicking on the Notifications link.

I am experiencing an issue when attempting to click on the notification link. Here is a snippet of the HTML code causing the problem:

<ul class="rghtSec fr menu logged"><li><a href="javascript:;">
  <div class="topIcon notify"><span>&nbsp;</span></div>
  <div class="mTxt">Notifications<span id="rJobCntr" class="rJobCntr"></span></div></a>
  <div class="subMenu recommendTT">
    <ul>
      <li><a target="_blank" class="blob" id="blobId" href="http://jobsearch.naukri.com/notifications">
Fetching jobs you may apply for</a></li>
    </ul>

I have attempted 5 different methods:

/*1*/ driver.findElement(By.xpath("//a[@class='mTxt']")).click();

/*2*/ driver.findElement(By.cssSelector("div[class='topIcon notify']")).click();

/*3*/ driver.findElement(By.linkText("Notifications")).click();

/*4*/ driver.findElement(By.xpath("//div[@class='pNotifyCont dspN']")).click();

/*5*/ Actions mouse=new Actions(driver);
   WebElement element=driver.findElement(By.xpath("//div[@class='pNotifyCont dspN']"));
   mouse.moveToElement(element).click().build().perform();

Error : 
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//a[@class='mTxt']"}
Command duration or timeout: 7.56 seconds

Unfortunately, none of these approaches are solving the issue :(, Any assistance on how to resolve this would be greatly appreciated!

Answer №1

If you want to access a link using its href attribute value, you can simply click on it by following this code:

driver.findElement(By.cssSelector("a[href*='notifications']")).click();

Alternatively, you can also use the below code snippet to click on the link directly:

driver.findElement(By.cssSelector("a[href=\"http://jobsearch.naukri.com/notifications\"]")).click();

Answer №2

Kindly update your xpath query as

driver.findElement(By.xpath("//div[contains(text(),'Alerts')]")).click();

The Alerts can be found within a div element.

To select an element under the Alerts section, you may use the following xpath:

driver.findElement(By.xpath("//div[contains(text(),'Alerts')]/following::a[1]")).click();

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

Purchasing a Checkbox alongside its corresponding label

I have been working with the Oracle CPQ tool and am faced with the challenge of creating a checkbox attribute within it. Upon inspecting the UI, I came across the following: https://i.sstatic.net/xE5aH.png Upon examining the browser source code (generat ...

The Foolish Mistake: Undetermined Dollar Sign

Can someone please help me with this issue? I've searched everywhere but I can't seem to fix the "Uncaught ReferenceError: $ is not defined" error. I have rearranged my script, tried putting it at the bottom, but nothing seems to work. Any sugges ...

How can you create a pop up window that automatically refreshes the original page when closed?

I am currently utilizing the most up-to-date version of jQuery. Upon clicking this button: <input id="ShowImages" class="" type="button" value="images"> The following function is triggered: $("#ShowImages").click(function() { $("#MusicianImag ...

"Click to view the latest data visualization using the Chart.js

I am exploring ways to enhance the animations in Chart.js for a new web project. The content is organized in tabs, with the chart displayed on the third tab out of four. Currently, the chart animates upon loading. How can I make it so that when #chartTrig ...

Simply tap on any part of the row to select the checkbox and bring attention to it?

Is there a way to make rows in a table selectable by clicking anywhere on the row? I have a script that displays checkboxes next to each result from a MySQL database, but I want users to be able to click anywhere on the row to select it and highlight the e ...

Show the entire image at its original size within an HTML5 Canvas of specific dimensions by utilizing JavaScript and potentially CSS styling

Is there a way to display an image within an HTML5 Canvas of a specific height and width while maintaining the image's aspect ratio? I need the image to appear at 100% size within a scrollable container. The image I want to use is of a tree: '&a ...

Using Node.js to seamlessly read and upload files with Dropbox API

I am utilizing the Dropbox API to retrieve a file from the node.js file system and then transfer it into a designated Dropbox folder. The transfer process is successful, but the file ends up being empty, with a size of 0 bytes. var path = require("path") ...

Ways to incorporate the CSS class name within the MUI createTheme palette color reference instead of using a hardcoded name

How can I dynamically insert a CSS class name inside the Material-UI(MUI) createTheme palette color instead of hardcoding it? const theme = createTheme({ palette: { primary: { light: "#66b53f", main: * ...

Guide to extracting a specific segment from req.body in Node.js

I've been attempting to access a specific nested property of req.body, but the output always turns out to be undefined. Here is the code snippet: let dataReceived = JSON.stringify(req.body); console.log(dataReceived); let refCode = ...

I am on a quest to locate the xpath for a day that is divided by the HTML line break element,

In my HTML structure, I have the following code snippet: <td class="FormLabel" valign="top"> <span class="DataLabel">Consists of:</span> </td> <td class="FormData"> <span class="BodyText"> Sunday<br> M ...

What is the most efficient way to modify the variables for numerous images within a JavaScript canvas?

I'm a newcomer to using Javascript in a canvas, and coding with javascript overall. My primary goal is the following: Create numerous fireballs (images) that spawn randomly with each having a fixed y-value and random x-value. The fireballs should t ...

Choosing the final element in a JavaScript array

In the process of developing an application that provides real-time updates on user locations and paths displayed on a Google Map, I have implemented a feature that tracks multiple users simultaneously via an object that receives updates every second. Pre ...

What is the method for sending a singular key (for example, 'k') to a window using Selenium WebDriver in Java?

As I create automated tests for a website that heavily relies on keyboard shortcuts for navigation, I have encountered an issue. While special key combinations like Ctrl + V, Alt + C work without a problem, sending single letters or num-pad symbols does no ...

Errors encountered by the Chrome extension

Hey there, I've recently delved into creating a chrome extension but hit a roadblock. My issue revolves around the service worker registration failing and encountering errors related to undefined properties. https://i.stack.imgur.com/bGzB4.png The c ...

jQuery's searchbar alert feature notifies users when there are no search results found,

I've created a simple search bar for my application. Currently, it searches users by data attribute, but if there is no match, it displays nothing. I would like to show an error message when no user is found. I tried using an if statement, but the err ...

Simple way to retrieve the first and last date of the current month using Node.js

I need help with retrieving the first and last date of the current month using the code below:- let currentDate = new Date(); let month = currentDate.getMonth() + 1; console.log(month); let firstDate = new Date(currentDate.getFullYear(), currentDate.getMon ...

How to make a 3D array in JavaScript and populate it

I am attempting to populate a 3D array with dimensions of 16x16x16, all filled with the color "rgb(255, 48, 144)". My goal is then to draw a rectangle using this color by referencing its position in the array ( arr[15][3][9] ). Here is my progress so far: ...

Is it possible to retrieve or modify the dimensions and position of an element set in a static position using only JavaScript?

Is it possible to access or modify the coordinates, width, and height of an element that is statically positioned using only JavaScript? If so, how can this be done in both pure JavaScript and jQuery? ...

What is the process of embedding a jquery function within a javascript function?

I'm currently using Javascript to validate my form, but I'd prefer to show the errors with Jquery. Is it possible to accomplish this? ...

Is it necessary to update the HTML lang attribute when switching between languages dynamically?

I have a one-page website that loads an AngularJS application supporting multiple languages. All the pages on the site are generated in the browser using JavaScript after the initial HTML is fetched from the server. I'm aware of the importance of se ...