Retrieving information from the Header element (<H></H>) with the help of Selenium

I have a website with the following code snippet:

<div class="list_item_normal">

  <div class="main_content">
    <div class="img_wrap">
      <a href="/home/Detaljer/9781118093757"><img alt="Miniaturebillede af omslaget til Operating System Concepts" src="/images/product_thumb/9781/118/093/9781118093757.jpg?1354045210" title="Miniaturebillede af omslaget til Operating System Concepts" /></a>
    </div>

    <div class="data_wrap">
        <h4>
        <!-- Added by sathiyaraj regarding E-book integration CR -->

        <a href="/home/Detaljer/9781118093757">Operating System Concepts</a> <small style='background-color:yellow;'></small>
        </h4>



        <h5>

         Abraham Silberschatz and Peter B. Galvin
          (2013)
        </h5>

        <div class="imprint">




        John Wiley & Sons, Limited
        </div>

        <div class="price">

           610,00 kr.        


        </div>
    </div>
  </div>

I am struggling to retrieve the content enclosed within the h5 tag. Despite my efforts, I am unable to achieve this. Here is the current piece of code I have been working on for the past 30 minutes:

    content = driver.findElement(By.xpath("//div[contains(@class, 'content')]"));
    List<WebElement> list
            = content.findElements(
                    By.xpath("//div[contains(@class, 'list_item_normal')]"));
    System.out.println(list.size()); // Just for debugging purposes
    for (WebElement e : list) {
        System.out.println(e.findElement(By.xpath("//h5")).getText());
    }

    driver.close();
}

This pertains to a book store where the author's name is found within the h5 tag. However, executing the above code yields a list of 10 identical authors, even though they are different in reality. I am unsure of where I am going wrong.

Could you please advise me on how to properly extract data from that specific tag?

EDIT:

You can view the entire page HTML here: http://pastebin.com/QALCvtaG

Answer №1

It seems like the issue lies in how you are trying to locate elements within other elements using xpath. To fix this, make sure to include a period before your xpath expression as shown below:

 System.out.println(e.findElement(By.xpath(".//h5")).getText());

You may also need to adjust the xpath in the following line of code:

List<WebElement> list = content.findElements(By.xpath(".//div[@class= 'list_item_normal']"));

The only modification made is adding a dot "." before each xpath statement to correctly target elements within elements.

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

Why does Selenium fail to retain input values as provided?

I am encountering an issue with Selenium: Although I have filled in the fields "Origen" and "Destino" successfully, the values disappear after the script runs. from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from sel ...

When the cursor is placed over it, a new div is layered on top of an existing

Currently, I am working on a project with thumbnails that animate upon hovering using jQuery. One of the challenges I have encountered is adding a nested div inside the current div being hovered over. This nested div should have a background color with som ...

Accessing an Android device from localhost using a hostname with ever-changing IP addresses

SCENARIO: I am currently running a local webpage on a WAMP server from my computer, which is connected to a wireless network. Additionally, I have an Android tablet also connected to the same network. OBJECTIVE: My goal is to access the local webpage on ...

What can I do to eliminate the excess white space within my div element?

I've built this app using the create-react-app CLI, but I'm struggling to remove the extra white spaces surrounding my main container. Check out the screenshot below for reference. Currently, the only div in my app has the following CSS: .main ...

Change a div to scroll vertically instead of expanding to accommodate its content

I am facing an issue with the layout I have created. It consists of a scrollable list of items in the center, with additional content above and below it in a column. The challenge is to make the list occupy all available empty space within the column witho ...

Tips for achieving thread safety with Selenium WebDriver in C#

Despite my efforts to search on Google, I was unable to find a solution. I am seeking help as I have found a solution in Java but not in C#. private static final ThreadLocal < WebDriver > webDriver = new ThreadLocal < WebDriver > () { ...

Is there a way to determine if the browser window is currently in use?

The code I am currently working with looks like this: let focus; function setFocus() { focus = true; } function hideWindow() { focus = false; } window.onfocus = setFocus(); window.onblur = hideWindow(); The intention behind this code is to use it in the ...

Can a video or image be displayed in HTML using an absolute path from a disk named E: while the localhost files are located in C:?

I'm attempting to construct a basic webpage utilizing HTML and PHP to display various videos, each video within its own videoplayer. The issue arises when I try to find the videos (they are stored in a folder on my secondary hard drive labeled E:&bso ...

Stylish CSS round link positioned on a half-circle

As a beginner in web design, I'm struggling with CSS. However, I have been given the task of creating a button like the one shown below. I am unsure of how to create a circular link like this. Any assistance would be greatly appreciated. ...

Ways to ensure the menu remains fixed on a fluid website

Is there a way to prevent the main-menu from moving to a second line when resizing the page down, while still maintaining fluidity? Thank you, Ken ...

What is the best way to navigate users to a different page when they click on a button?

I recently designed a button in Dreamweaver software. Could you please guide me on how to set up the button so that when clicked, it redirects the user to a different page using Dreamweaver? Below is the HTML code for the button: <input type="submit" ...

Tap the mobile menu button twice to toggle the menu

Incorporating: Visual Studio 2013, ASP.NET (VB) Webforms. Upgrading an existing WebForms website from .NET 2.0 to .NET 4.5 and implementing mobile-friendly features for better Google support. I have added a button to the top right corner as follows: < ...

selenium.common.exceptions.InvalidArgumentException: Error: the specified argument is not valid - incorrect locator provided

Hey there, I've been working on creating a bot using selenium and encountered an issue with the send keys function. Despite trying for hours, I can't seem to figure out how to make it work. If anyone has any insights or solutions, I would greatly ...

When the previous textbox is filled, the cursor will automatically move to the button

Utilizing an RFID reader where students tap their ID to display basic info, a hidden button on the form opens a modal with various purposes for selection. The challenge is shifting focus of cursor to the button and automatically clicking it when the last ...

Is there a way to give a unique color to a single <a> element with a specific class, differentiating it from the

Apologies if my title is not precise enough. Describing this in just a few words for the title was a bit challenging. Context I am personalizing a template on wordpress.com. The website in question is: I have enclosed the DONATE menu item in a gold-col ...

Evaluating mirroring of user input between two input fields using Selenium and Python

Keen on creating a Selenium test that verifies text entered in one field appears mirrored in another? Here's the code snippet: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","s ...

Increase ng-grid row height dynamically based on content without any external plugins or reliance on jQuery

I came across a similar question on this topic at Angular ng-grid row height However, none of the solutions provided there meet my requirements. If I use CSS to fix the issue, it impacts the page's responsiveness and disrupts ng-grid's header fu ...

Toggle the functionality of the login button in foundation

I am currently utilizing the front-end framework Foundation. My question is, how can I modify this menu so that the Log in button is only enabled when both the Login and Password input fields are filled with text? <form data-abide="ajax"> <div ...

failure of svg spinning

I'm currently working with an SVG file and attempting to incorporate a spinning animation similar to loaders and spinners. However, I am facing an issue where the rotating radius is too large and I am struggling to control it effectively. CSS: .image ...

Functionality Issue: Submit Button Not Working on Designed Form Select

Through dedication and hard work, I managed to create a customized form with images that display correctly in Firefox, Chrome, and Internet Explorer's compatibility mode. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...