Selenium Assistance: I'm encountering a scenario where on a webpage, two elements share the same Xpath, making it difficult to differentiate them based on even

At index [1], both elements are identified, but at index [2], nothing is identified. The key difference between the two is that one has display:none, and the other has display:block. However, their involvement in determining these fields is minimal due to Xpath as the fields are sourced differently.

Despite having identical elements, these divs have a document attribute which means the Xpath for them does not start with the div itself but rather after a certain part of the document. Below is an example code snippet:

<div name='some-name' style="display:none;">
  ...
  <!document>
  <div id='some-id'>
    ....
    <button name='some-name-2'>some-button-name</button>
    ....
  </div>
  ...
</div>
<div name='some-name' style="display:block;">
  ...
  <!document>
  <div is='some-id'>
    ....
    <button name='some-name-2'>some-button-name</button>
    ....
  </div>
  ...
</div>

Thus, their Xpath expressions will look something like:

[id='some-id']/...../button

Selecting ([id='some-id']/...../button)[1] retrieves both elements.

But selecting ([id='some-id']/...../button)[2] retrieves none of them.

Update

In this scenario, I had two iframes and the issue was due to difficulty in switching between them. This has been resolved now. Thank you!

Answer №1

When using Xpath, the technique involves locating the element with id='some-id' and then pinpointing either the first or second button within that same element. By adjusting the positioning of [1] and [2] to appear after the [id='some-id'] instead of at the end of the path, you gain the flexibility to target either the first or second occurrence of [id='some-id'], followed by the respective button within that specific element.

Answer №2

It is important to understand that unless the two elements are truly one and the same, there will always be different XPath expressions for each. Finding the right XPath expression depends on the variability of the content on the page. If the data is fixed, then reading it may not even be necessary as everything is already known.

In regard to your suggestion that subscripts [1] and [2] do not work, it would be helpful to see the actual expressions you used. The example you provided, ([id='some-id']/...../button)[1], is not a valid XPath expression at all.

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

Load the dropdown menu with JSON data but encounter an error: SyntaxError caused by an unexpected token `{` in

I am currently working on populating a dropdown menu with the values of the 'styleName' field from a JSON data file. Here is an example of my JSON data: {"name":{"styleName":"name","fillType":"none","fillTrans":"0","outlineType":"solid","outlin ...

The dynamically inserted nested division within a bootstrap grid does not occupy the entire width of the parent division

I am working with a parent div where I am dynamically adding smaller divs with content to achieve a specific layout. Here's how the structure looks: https://i.sstatic.net/tN1s1.png The code for the parent div is shown below: <div class="row" sty ...

Tips for customizing table cell styling in editable cells with React material table

My code utilizes a material table with editable cells. However, I am encountering a strange style issue when I edit a cell in the table. Please refer to the image below. Can anyone suggest a solution to fix this problem? https://i.sstatic.net/Miiov.png ...

Converting jQuery drag and drop functionality to Angular: A step-by-step guide

I have implemented drag and drop functionality using jquery and jquery-ui within an angular project. Below is the code structure: Index.html, <!doctype html> <html lang="en"> <head> <link href="//code.jquery.com/ui/1.10.3/themes/ ...

HTML/ModX styling for selected textboxes

I am looking to enhance the style of my search-box, which is similar to the one on THIS website. While I have tried using :active, the effect only lasts for a brief moment. Currently, my code is heavily influenced by modX terms, but I will still share it h ...

Adjust the filter settings in angularJS

I am working on a request page with three buttons: Open, Close, and All. Currently, when the page is opened, it displays the 'All' requests by default. However, I want to change this to show the 'Open' requests instead. I want users to ...

Retrieve the JSON element from a Backbone model by its unique identifier

I'm diving into Backbone for the first time and I'm facing difficulties making it work smoothly with a JSON data file. Here's how my model looks: window.Test = Backbone.Model.extend({ defaults: { id: null, name: null, }, url: f ...

Navigational Scroll Parallax Issues

I have been trying to implement a basic 2 layer parallax effect using ScrollMagic on my index page. The layout consists of a large image with content below it, but I am facing some challenges. Despite going through the documentation and source code multipl ...

Is it possible to change the hover highlight rotation on a link without affecting the surrounding elements?

Is it possible to rotate the highlight on a link when hovered? I'm new at this, so apologies if this question seems basic. This is how my css/html is currently structured: .links { display: block; } .links a { color: #000000; text-decoratio ...

Utilizing Vue-cli 3: Incorporating static assets with a specific path

My application is located at: https:/firstpath.com/firstsubfolder/app.html When using relative paths, my static assets are being loaded from: https:/firstpath.com/firstsubfolder/img/image.png Is there a way to specify a specific path for my static asse ...

Various instances of the jQuery niceScroll plugin

I successfully set up jQuery niceScroll on the body, but now I want different themed scrollbars on one page. Below is my code snippet: Here is my fiddle: http://jsfiddle.net/JPA4R/135/ <script> $(document).ready(function () { $("body").niceScroll ...

Combining arrays using the jQuery .each() function

I'm attempting to generate an array from several div id's. Check out my code below: $(function(){ $('#content > div[id^=post]').each(function(){ var ele = Number($(this).attr('id').substr(5,4)); var arr ...

Images in the Ionic app are failing to display certain asset files

Currently, I am working on an Ionic 4 app for both Android and iOS platforms. The issue I am facing is that only SVG format images are displaying in the slide menu, even though I have images in both SVG and PNG formats. public appPages = [ { ...

Creating a full width and height slider with a header menu that is responsive to all device screen sizes

Looking to create a full-width slider with a header that adjusts for all screen sizes, especially larger devices. I've been searching online but haven't found a satisfactory solution yet. If anyone has any ideas or tips on how to achieve this, pl ...

Send Selected Input From Radio Button To Form Using a Function

Within the main php page titled tipComp.php, there is a script designed to submit a selected value from a radio button within a form: <script> $('input[type=radio]').on('change', function() { $(this).closest("form& ...

Losing value in Angular service when the view is changed

I am currently working on a project to create a basic Angular application that retrieves data from Instagram. The concept involves the user inputting a hashtag on the main page, which then redirects them to another page where posts related to that hashtag ...

Creating linear overlays in Tailwind CSS can be achieved by utilizing the `bg-gradient

Is there a way to add a linear background like this using Tailwind CSS without configuring it in tailwind.config.js? The image will be fetched from the backend, so I need the linear effect on the image from bottom to top with a soft background. Here are ...

Introducing React JSX via a nifty bookmarklet

Looking to convert code found in an HTML file into a bookmarklet? Here's the code snippets involved: <script src="JSXTransformer-0.13.3.js"></script> <script src="react-0.13.3.js"></script> <script type="text/jsx;harmony=tr ...

What is the best way to retrieve data from a JSON object?

Can the status variable be used as a JSON object? What is the method to access the values of action_success and newIndex within the status object? Server: [HttpPost] public ActionResult UploadFiles() { // save file.. return Json(new { action_suc ...

Retrieve information about a parent's data related to child events in Vue.js

I'm working with two components nested inside each other. The parent component has a click event that needs to modify the data value of the child component. <template> <div> ..... ..... <my-component :op ...