What is a method for choosing information from a webpage without relying on the written words?

Is there a way to select and click the blog title without relying on the text, which can change frequently? Unfortunately, I don't have an "li" tag, so my previous methods aren't working in this scenario. Any assistance would be greatly appreciated.

Here is the latest approach I've attempted:

driver.findElement(By.xpath("(//h2[contains(@class, 'blog-post-title')])[3]/a")).click();

<!DOCTYPE html>

<html>
<head>
    <title></title>
</head>

<body>
    <div class="view-content">
        <div class="views-row views-row-1">
            <span class="pubdate">July 31, 2012</span>

            <h2 class="blog-post-title"><a href=
            "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">The
            Surprise Attack (And When Not to Do It)</a></h2>

            <p class='field-summary'>With 200 meters remaining in the
            250-kilometer Men’s Olympic Road Race last Saturday, Colombia’s
            Rigoberto Uran made a fatal mistake: He turned his head to see what
            was happening behind him....</p><a class="read-more" href=
            "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">Read
            more</a>

            <ul class="links inline">
                <li class="datetime first"><span>Posted at <time class=
                "timestamp" datetime="2012-07-31T17:41:04-04:00">5:41
                PM</time></span></li>

                <li class="disqus_comments_num"><a data-disqus-identifier=
                "node/28978" href=
                "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it#disqus_thread">
                Comments</a></li>

... [additional unchanged HTML content] ...

        </div>
    </div>
</body>
</html>

Answer №1

When using Jquery, you can utilize standard CSS style selectors like class and id. For example:

// Class
$('.my-class')
// Id
$('#my-id')
// Element
$('div')

As for handling clicks...

// You don't necessarily need to pass the event parameter, but it can be helpful
$('.my-class').click(function (event) {
    /* Do something */
});

I hope this information proves useful. A quick Google search will reveal even more possibilities. :)

Answer №2

To retrieve the specific element, consider utilizing the following query:

(//h2[@class='blog-post-title'])[position()=1]
. In this code snippet, position() dictates the exact number of the element to be captured.

Answer №3

Experiment using xpath locator and providing a specific index

By.xpath("(//h2[@class='blog-post-title']/a)[3]")

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

the best way to transfer a JavaScript value to a PHP page

I am experiencing an issue with an undefined function in my PHP page. I have tried everything and it just doesn't seem to work for me. Can anyone please assist me with this problem? <!DOCTYPE html> <html> <head> <scrip ...

Dynamic Material UI TextField underline width using JSX in React

I recently encountered an issue while trying to add padding to my TextField component. I applied the padding to the root 'style' property, but it caused the underline to overflow beyond the designated area to the right by the same amount as the p ...

Is there a button that allows updating the text in a search field by entering multiple values one after the other?

Is it possible to insert multiple values sequentially into the search field using the same button? For example: value 1, value 2, value 3... Check out a demo here <form id="form1" name="form1" method="post"> <p> <input type=" ...

Tips for concealing password input in an HTML form

Is it possible to echo the password variable into an input field in a form without displaying any password characters in the HTML source code? The purpose is for the form to be able to post the password without revealing it. Any ideas on how to accomplis ...

Implementing Custom Font Awesome Icons in Your Angular Project

I recently upgraded to a fontawesome subscription with a paid plan and have successfully created some custom icons. Now, I'm looking to integrate these icons into my angular app. Here are the dependencies listed in my package.json file: "@fortawe ...

Experiencing a syntax error in a query despite being confident that the query is accurate

When attempting to deploy my ear file on glassfish, I encounter the following error: 2023-11-23T10:46:59.388+0100|Severe: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method 2023-11-23T10:46:59.389+0100|Severe: Exceptio ...

I’m having trouble getting my home.html page to render in Spring Boot

https://i.sstatic.net/ovw5z.jpg This image perfectly encapsulates the essence of my code and project structure. I am encountering an issue where I cannot return the home.html file located in the static folder. I have tried specifying it as 'home&apos ...

Serialization with Gson based on field values

I have a custom Java object called MyGsonPojo with fields valueOne, valueTwo, and valueThree. I need to serialize this object into a JSON body for a server call, but some fields are optional and the API reacts differently based on default or null values (u ...

The HTML element failed to be inserted

Currently, I am involved in a project based on .NET Core for my organization. This project entails loading work orders from our SQL database using Entity Framework and then filtering them to display as markers on a map via the Google Maps API for our insta ...

Experiencing a problem with XAMPP? Changes made to the code are not reflected after saving

Recently, I've encountered a strange issue with my XAMPP test server while working on a game project. Everything was running smoothly until I noticed that when I make changes to certain files in Notepad++ and save them, the updates are not being refle ...

Troubleshooting: Custom checkbox using CSS ::before does not display properly on Firefox and Edge browsers

Encountering a frustrating CSS challenge while working on a project that needs to be compatible across various browsers. Specifically, I'm having issues with custom styling for checkboxes. Despite following recommendations to use a ::before pseudo-ele ...

Various Ways to Add Hyperlinks in HTML Using CSS Styles

Does anyone know how I can link multiple HTML files to one hyperlink, where only one file opens randomly when clicked? I am currently using . Your assistance would be greatly appreciated! I've tried searching online for a solution, but haven't ...

What is causing the input boxes to exceed their container boundaries so drastically?

My plan was to organize 4 input text boxes in 4 equal columns on a single row of the form. Using Bootstrap's grid column classes, I set col--3 for medium and large screens for those four inputs. For small and extra-small sizes, I designated them as co ...

Tips for personalizing the Material-UI sticky header to work with horizontal scrolling

Check out this example of a Material UI table with a sticky header on CodeSandox: https://codesandbox.io/s/yi98d?file=/demo.tsx I encountered an issue where the left side header cells slide behind each other when I added a stickyHeader prop to the Materia ...

Can you help me identify the reason behind a page displaying a temporary horizontal scrollbar?

While browsing the following page in Google Chrome: A horizontal scrollbar appears briefly during page load. It seems that something hidden below the fold is causing this issue, but I haven't been able to identify the exact cause. EDIT:- Watch a v ...

Is it possible to use WHILE loops twice in PHP?

Just starting out with PHP and hoping for some assistance. I have a MySQL database table with columns for "id", "img", "link", and "desc". My goal is to echo all of this data in the following format: <div id="featured"> <a target='_b ...

Implementing many-to-many relationships in JPA/Hibernate can be done when the join table has its own primary key

I have a scenario with three tables of simple structure: pub [id, name] days [id, name] pub_days [id, pub_id, days_id] Interestingly, someone decided to add their own primary key to the pub_days table in addition to the compound identity (pub_id + days_i ...

Tips for utilizing the "rel" attribute in conjunction with an image map

I have been successfully using a jQuery popup to display different DIVs when clicking on different links. However, I now need it to work with an image map. It appears that the "rel" attribute is not functioning properly with the image map. Check out the ex ...

Customizing the search dialog in JQGrid to adjust the width of the input fields

Can the width of the text box in Jqgrid default search dialog be increased? Check out the image: Updated Code for the search functionality: jQuery("#subscriptions").jqGrid( 'navGrid', '#pager', { del: false, add: false, e ...

Verification symbols in Unicode

I'm on the hunt for a universal checkmark symbol that is compatible across various operating systems and web browsers. I've tested out the following two options: Version 1: ✓ Version 2: ✔ (source) However, these symbols seem to be working ...