Trigger an Onclick Event in Selenium

I am attempting to click on a menu icon with the following HTML code

<a href="#" class="ctm-icon-link" onclick="show_menu('doc_107094', 1); return false;"><i class="icon-left-space icon-chevron-sign-down">&nbsp;</i></a>

The document number will change each time. Here is my code to click the menu icon:

FindElement(By.ClassName("ctm-icon-link"));
FindElement(By.XPath("//a[@href='#']@onclick"));
FindElement(By.XPath("//a[@href="#"]@class"));

Answer №1

If you want to find the element using the onclick event, you can apply WebDriverWait with the elementToBeClickable() method and choose one of the Locator Strategies:

  • XPath 1:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='ctm-icon-link' and starts-with(@onclick, 'show_menu')]")));
    
  • XPath 2:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='ctm-icon-link' and starts-with(@onclick, 'show_menu')]/i")));
    

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

Tips for executing an npm command within a C# class library

I am currently developing a project in a class library. The main objective of this project is to execute a JavaScript project using an npm command through a method call in C#. The npm command to run the JavaScript project is: npm start The JavaScript ...

Best practices for managing errors and exceptions in frontend React and backend .NET/C#

My React frontend communicates with a .NET/C# backend. When the user clicks a button, an integer is sent to the backend. If the number is positive, everything runs smoothly. However, if it's less than 0, an exception occurs. I know how to handle res ...

Guidelines for automatically installing npm modules during the building of an ASP.NET Core project

In my current project, I am using ASP.NET Core Web API as the backend and React SPA as the frontend. The folder structure is organized like this: WebApiProject │ ├── ClientApp/ │ ├── public/ │ ├── src/ │ └── package.j ...

Initiate a react change event using Appium

I'm currently working on automating a hybrid app that has a login screen implemented as a react web view, and unfortunately, I don't have control over it. The challenge I'm facing is that the Sign-in button remains disabled until something i ...

Guide on how to use Selenium to drag and drop a canvas web element specifically in the Chrome browser

Having trouble moving an image within the canvas web element (avatar editor) on Chrome using Selenium. Check out this canvas element: Watch a quick demo of what I'm trying to accomplish with Selenium Webdriver: Please review the code snippet below. ...

Developing React applications using Behavior Driven Development with a personalized API call for simulating mock

Currently, I am utilizing BDD with cucumberjs, selenium-webdriver & react. Within a specific scenario, I wish to execute a Mock API call rather than an actual API call. For instance, I am currently making a call to: http://example.com/v1/getData However ...