Tips for locating the position of a child HTML tag in Selenium WebDriver

Is there a way to retrieve the index of an HTML child tag based on its XPath? I encounter this situation with three elements in the right rail of a page:

//*[@id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[1]/h4

//*[@id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[2]/h4

//*[@id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[3]/h4

If I have located the first element and want to extract the number inside the div tag, which is '1', how would I go about doing so?

I attempted to find a solution from a past post (How to count HTML child tag in Selenium WebDriver using Java) but am still struggling to grasp it.

Answer №1

To extract the number, you can utilize regular expressions:

var regExp = /div\[([^)]+)\]/;
var matches = regExp.exec("//[@id=\"ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880\"]/div[2]/h4");

console.log(matches[1]); \\ this code will output 2

Answer №2

If you need to retrieve all the previous reports before the current one using xpath, you can use the following syntax:

//h4[contains(text(),'hello1')]/preceding-sibling::h4

To determine the index of your current report, simply count the number of preceding reports plus the current one.

Another approach is to capture all the reports at once and iterate through them to find the desired content. The sequence of reports reflects the order in which they appear in the dom structure.

For Java, a code snippet could be as follows:

List<WebElement> reports = driver.findElements(By.xpath("//*[@id='ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880']/div/h4"));
for(WebElement element : reports){
    if(element.getText().contains("report1"){
         return reports.indexOf(element) + 1;
    }
}

If not, manual parsing of the xpath may be necessary to extract the required value (refer to LG3527118's response for guidance).

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

Discovering content between HTML tags using Python Regular Expressions

Can someone help me with extracting the content between HTML tags shown here: <div class="td-wrap"> <a href="/universities/university-cambridge" class="uni-link">University of Cambridge </a> </div> ...

What is the best way to display the letter X (Clear) in the Chrome INPUT field with the type

A custom application was developed that utilizes an input field of the number type: <input type="number" min="-1000000" max="1000000" step="0.01"> Within Internet Explorer, there is a small "clear" or [X] button on the right side of the input field ...

Tips for preserving alignment amid fluctuations in window size using CSS and HTML

Being new to using CSS for webpage design, I'm currently facing some alignment challenges. The issue arises when I resize the window in which my webpage is displayed, causing the elements on the page to lose their alignment due to fixed widths being s ...

What is the best way to extract data from the ajax.done() function?

My question revolves around the function shown below: $.ajax({ url: "../../getposts.php" }).done(function(posts) { var postsjson = $.parseJSON(posts); }); I am wondering how I can access the variable postsjson outside of the .done() function sco ...

What is preventing this Node JS code from successfully serving a static PNG file?

To the administrators, I have spent hours scouring various resources for answers related to Node Js, Express, and serving static files. Despite my efforts on stackoverflow and other platforms, I have yet to find any solution that works for me. If my questi ...

Jsoup is ensuring that iframe content is properly rendered and secured

Having some source HTML that needs parsing <div> <iframe><script>alert('hello')</script></iframe> </div> After using Jsoup to parse and printing the body HTML, this is what I get. <div> <iframe>& ...

All images must be arranged to fit seamlessly side by side regardless of the screen size

I'm currently experimenting with creating a website dedicated to my favorite TV shows. Upon entering the site, you are greeted with the main page that includes 7 images. Each image is linked to a specific webpage providing information about the corre ...

Resolving the problem with highlighting borders

I am facing an issue with 2 inputs displayed side by side as shown below: .centered-query-cn { height: 150px; display: flex; } .browse-query { display: flex; } .browse-query input { display: flex; flex-grow: 1 !important; } <link href="ht ...

Requiring a compulsory field in PHP and MySQL

I am facing an issue with the following query: $tww_update_sql = "UPDATE `telesales_anc` SET `Ime` = '".$_POST['Ime']; I also have a form with a submit button structured like this: <form id="contact" method="post" action="ANC_pozivanje ...

Creating visual aesthetics for code generated by JavaScript

I have implemented a JavaScript function that performs a "find and replace" operation, changing all the backticks in the HTML to <code> tags, and replacing all the pound signs with </code> tags. However, I am encountering an issue where my styl ...

Setting the default font size in CKEditor is a helpful feature that allows you to

I'm struggling to set a default font size for a specific CKEditor Instance. Despite my efforts in researching online, I haven't found a solution that addresses my issue. On a webpage where users can input website content, there are three Editor ...

it is not possible to define the button border style using css

Encountering an unusual issue, where I am able to customize the color, font-size, and background of a button using CSS, but unable to set its border. Below is the HTML and CSS code snippet: .btn-general { border-width: 2px !important; border-style: ...

What is the best way to choose a particular user from the database and display only their information?

I am looking to create a table that displays user data from my database. In essence, I have set up an auto-increment "ID" field in my users' table and now I want to include a multiple-choice box where all the users in the database are listed. These u ...

Eliminating the border around a textarea in HTML

Currently, I am dealing with the textarea tag in HTML and am trying to eliminate the border surrounding the box. Additionally, I am aiming to position the text at the bottom of the textarea. ...

Adjust the text color within an input field or a text area

I'm looking to customize the text color inside an input field, specifically making it white. Here is a reference image to demonstrate what I mean: https://i.sstatic.net/JJomd.png Below is the code snippet that achieves this effect: <v- ...

"Implementing a function to automatically input a selected value from a dropdown menu into a text field

I'm facing an issue and need some help. I have a form with one select field and one text field. What I want is for the text field to display a value corresponding to the option selected in the select field. For example, if I choose "B" from the select ...

Toggle the visibility of a div element and smoothly scroll to it on the page

Upon clicking the form, my goal is to show or hide it and scroll down to view the form. The current code I have in place seems to work after the first click. However, on the initial click, it shows the form but fails to scroll down. Any insights on what I ...

Issue encountered while displaying information in a javascript/HTML document

My JavaScript code is pulling data from a fusion table and displaying it on an HTML document. However, I'm encountering an error in this specific line: <a href="data[row][4]">" + data[row][4] + "</a>\ I'm completely stumped as ...

Adjust the size of the pagination for subsequent pages in posts

Could someone please assist me with changing the size of post pagination that includes the code --nextpage--? I've attempted the following code: .td-post-content .page-nav a { font-size: 16px; } Unfortunately, it isn't working as expected. App ...

Leveraging jQuery Ajax and MySQL to generate dynamic HTML content

I'm in the process of creating a unique photo gallery that utilizes dynamic features. Instead of relying on constant HTML/PHP refreshing for updates, I am incorporating jQuery to handle dynamic MYSQL queries. As a beginner, I've managed to creat ...