Looking for the xpath or css required for the given HTML snippet

In the following HTML code, a pop-up is generated to add a product. I am looking for an XPath or CSS selector that works in Selenium and allows me to click on the "add" button to add a new product.

I have tried using the XPath //div[18]/div[2]/div/button, which only works for one session. When the browser is closed and reopened, the above XPath no longer works. So, how can we write an XPath that does not require specifying div[number] like in the case of div[18]?

<div style="height: auto; min-height: 104px; width: auto;" class="dialog ui-dialog-content ui-widget-content">
<fieldset><legend>search</legend>
<div id="nameorids">
<input type="radio">name
<input type="radio">id
</fieldset>
<div style="clear: both; padding-top: 5px; float: right;"><button class="add">add</button></div></div>

Answer №1

Initially, it's important to note that this HTML is not valid: the closing tag for the nameorids div is missing before the fieldset closes. Could this be a typo?

If you have control over the HTML, consider adding an id or class attribute to the outer div so that you can target them using a CSS selector. For example, if you adjust the HTML to look like:

<div id="foo" ...

You could then utilize the selector #foo button.add.

Answer №2

When it comes to identifying nodes, there are numerous XPaths that can be used. For instance, is the button in question the only one with the class "add"? If so, the XPath would look like this:

//button[contains(concat(' ', @class, ' '), ' add ')]

Just having a snippet of the HTML isn't all that helpful.

If you're able to access the page and use Chrome, try using my extension here. After installation, restart Chrome and then right-click on the button. Select

PsychoXPath -> Element (Short)
to automatically generate a unique path for the element and copy it to your clipboard and console.

Answer №3

If we were to style a button, it would have the CSS class of button.add

webdriver.findElement(By.cssSelector("button.add")).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

How can you replicate a mouseover event using Selenium or JavaScript?

I have recently been working on a task involving web UI automation using Selenium, Javascript and SeLion. My goal is to capture a screenshot of a scenario similar to the Google homepage, specifically focusing on the "Search by voice" feature when hovering ...

Utilizing nested Bootstrap 3.1.1 collapse functionality

Hey there! I need some assistance. Currently, I am working with Bootstrap 3.1.1 and implementing nested collapse menus in a sidenav. My goal is to apply a background color (using a specific class) to the open menu and remove it when the menu is closed. Ev ...

The header section on a website not behaving as expected in the Chrome browser

For my website, I want it to look like this in Internet Explorer: https://i.stack.imgur.com/hSkVM.png ^^ This is the IE view However, in Chrome, it displays like this: https://i.stack.imgur.com/noQw8.png The issue is with the Top bar on Chrome showing co ...

Stopping halfway through a jQuery toggle width animation to close again

Perhaps the question may be a bit unclear, but allow me to provide an example. When repeatedly clicking the button that toggles the width, the div continues to animate long after the button press, which is not visually appealing. My desired outcome is for ...

Acquiring the console log data from Firefox version 43 using Selenium, all without the use of any

Can Selenium retrieve the console.log from browsers like Firefox 43? If so, how can it be done? My settings are as follows: DesiredCapabilities capabilities = DesiredCapabilities.firefox(); LoggingPreferences logs = new LoggingPreferences(); logs.enable( ...

HTML - maintain centered text positioning while implementing padding on the left side

Here are the HTML and CSS code snippets I am working with: #page { background-color: #000; color: #fff; height: 360px; width: 360px; min-height: 100%; } #viewport { font-size: 20pt; text-align: center; } .taskTitle { height: 13%; fon ...

Issue with fancyBox not functioning properly when integrated with a caption script

Hey there! I've been working with both jQuery's fancyBox for displaying image/video galleries and the Capty script for showing image titles on images. However, I've run into a snag - when the title is displayed on the image, fancyBox st ...

What is causing the Font Awesome icons to not display in this HTML code?

Utilizing Font Awesome in HTML is my goal, however, the code snippet provided is not rendering the desired outcome. I have stored the Font Awesome directory within my project folder as well... <?php include '../config/sessionhandling.php'; // ...

ElementNotInteractableException raised by OpenQA.Selenium: unable to interact with element

During my testing, I encountered an error when running a test with a searcher that I had previously used without any issues: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> OpenQA.Selenium. ...

Locate an element on a fresh webpage by utilizing Selenium WebDriver in Java

I'm currently working on automating a process that requires opening a new tab and performing actions on that page. However, my code is still targeting elements on the previous tab instead of the new one. I've attempted to switch tabs using the co ...

How to vertically center Bootstrap 4 columns while maintaining uniform height

Having trouble with aligning bootstrap 4 columns' height and vertical alignment? Check out this JSFiddle to see the issue. I'm working on designing a layout with items in both desktop and mobile views, all within borders. The challenge lies in ...

The jQuery function is not functioning correctly

Currently, I am in the process of setting up an accordion menu using jQuery and CSS3. Everything is working perfectly fine so far except that the menu always opens upon page load and the code to hide it is not functioning as intended. Here's the link ...

A compilation of Selenium IDE commands that can be exported to Java/WebDriver

tag, I am facing a challenge with exporting tests from Selenium IDE to Java. When attempting the export for the first time today, I encountered unexpected difficulties due to unsupported commands that were not included in the generated Java code: // ERRO ...

How To Make Divs Flow Smoothly With CSS

Here is the code snippet I am working with: /* Coded by @fitri This ReactJS project component was created with love https://codepen.io/fitri/full/oWovYj/ */ function enableDragSort(listClass) { const sortableLists = document.getElementsByClassNa ...

How do you center controls within a repeater control's item template?

I have a repeating control on my page that displays an image and a hyperlink one below the other. When I add more than one line of text, it causes the image to move up. I want the top of the images to be aligned horizontally and the text to flow downward i ...

CSS to target every second visible tr element using the :nth-child(2n)

My table has a unique appearance (shown below) thanks to the application of CSS nth-child(2n). tr:nth-child(2n) {background-color: #f0f3f5;} https://i.sstatic.net/1AnDi.png I made some elements hidden on the vID, ID, and MO_Sub tr. <tr style="displa ...

Tips on linking CSS files within a jade layout

I am having trouble referencing custom.css and bootstrap.min.css in my jade layout file that refers to views. It seems like the references are not working correctly. Can somebody please review my code to see if there is any issue? Jade Layout doctype htm ...

Tips on improving the performance of Browsermob Proxy

I've been experimenting with Browsermob Proxy and a basic script utilizing it (shown below) is running very slowly. It seems like the delay could be due to the time it takes to initialize a new server. If that's the issue, is there a way to init ...

Steps to set a default selection from a list of components in Angular 7

Currently, I am in the process of developing a messages page layout where users can select a message from a list on the left side and view its content on the right side, similar to how it is done in Outlook. Additionally, users should be able to reply or c ...

Maintain a consistent size for the material UI drawer, preventing it from resizing when the content size fluctuates

Incorporating Material UI into my project, I decided to use a drawer for navigation. However, within the drawer, there are Collapsible lists that expand when clicked. The issue arises when the list text items are lengthy, causing the drawer to unexpectedly ...