Is there a way to retrieve the parent id from HTML code when using Selenium WebDriver with Java?

Is it possible to retrieve the parent ID in the provided HTML code using the class name "description"? All div elements have a common class name of "card-box" and their IDs are generated during runtime.

<div id="2321-79" class="card-box"></div>
    <div id="2321-32" class="card-box"></div>
    <div id="2321-33" class="card-box"></div>
    <div id="2321-81" class="card-box"></div>
    <div id="2321-74" class="card-box"></div>
    <div id="2321-59" class="card-box"></div>
    <div id="2321-80" class="card-box"></div>
    <div id="2321-35" class="card-box"></div>
    <div id="2321-84" class="card-box">
        <div class="card overdue white">
            <div class="summary"></div>
            <div class="info" style="display: none;"></div>
        </div>
    </div>
    <div id="2321-82" class="card-box">
        <div class="card overdue white">
            <div class="summary">
                <a class="pic" href="/profiles/219"></a>
                <div class="detail">
                    <div class="title"></div>
                    <span class="description"></span>
                </div>
            </div>
            <div class="info" style="display: block;">
                <div class="team">
                    <table width="100%" style="padding: 0px 10px">
                        <tbody>
                            <tr>
                                <td class="from" colspan="4">


                                                  From


                                    <a href="/profiles/219"></a>


                                                  to


                                    <a href="/profiles/1"></a>
                                </td>
                            </tr>
                            <tr>
                                <td width="20%"></td>
                                <td width="50%"></td>
                                <td width="7%"></td>
                                <td width="20%"></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="actions">
                    <form method="post" data-remote="true" action="/commitments/get_view" accept-charset="UTF-8">
                        <div style="margin:0;padding:0;display:inline"></div>
                        <input id="col_type" type="hidden" value="col_my_work_commitments" name="col_type"></input>
                        <input id="card_id" type="hidden" value="174" name="card_id"></input>
                        <input id="state" type="hidden" value="published" name="state"></input>
                        <table width="100%">
                            <tbody>
                                <tr class="actions">
                                    <td>
                                        <button id="commitments-identifier" type="submit" name="btn_discuss"></button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

I attempted this code for the aforementioned task:

String val1 = "Final";
List<WebElement> resultList1 = card1.findElements(By.className("description"));
for (WebElement resultItem : resultList1){
       String tabname=resultItem.getText();
       if(tabname.contentEquals(val1))
       {
           System.out.println(description.getAttribute("id"));
           resultItem.click();                 
           break;
       }
}

Answer №1

If you have a specific WebElement el and you are looking to retrieve its parent WebElement, you can achieve this by using the following:

el.findElement(By.xpath(".."))

To obtain the id of that element, you can utilize the following code:

el.findElement(By.xpath("..")).getAttribute("id")

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

Instructions on utilizing the initialized Webdriver instance from the Base class in a child class in Java

public class Base { public WebDriver driver; @BeforeMethod public void setUp() { driver = new FirefoxDriver(); } } public class CustomFunctionality extends Base { public CustomFunctionality(WebDriver driver) { ...

Discover: The best practices of utilizing different location strategies

When utilizing Selenium WebDriver, when assigning your Locators with @FindBy, you have the option to select between How + using: @FindBy(how = How.ID, using = "foobar") WebElement foobar; or you can directly utilize the location strategy like this: @Fi ...

When running a test using Selenium with Python and Unittest, an error message may occur stating "Another element is currently receiving the click"

Question: I am currently working on a project where I am creating test cases for Python and Selenium in Magento. As part of this, I need to click on a slider button. Code Snippet: def test_pagebuilder_slide_button_pagebuilder_button_primary(self): ...

Selenium htmlUnit with dynamic content failing to render properly

My current project involves creating Selenium tests for a particular website. Essentially, when a user navigates to the site, a CMS injects some dynamic elements (HTML + JS) onto the page. Everything works fine when running tests on the Firefox driver. H ...

Can anyone provide a solution for sorting web elements? I'm facing challenges with the drag and drop feature when trying to sort items

I'm facing challenges with sorting web elements using drag and drop functionality. Despite having what I believe to be the correct logic, my drag and drop isn't working as expected when running the code. public void sortable() { // Issue with lo ...

Extracting images using web scraping that are only loaded when they come into view due to lazy loading

Struggling to extract all the feed item images from a website with infinite scrolling? Look no further than this link. Despite trying several solutions found online, nothing seems to work for me. Seeking answers for a similar problem? Check out: How to ...

Learn how to use the `getElementsByClassName()` method in Selenium

Currently, I am utilizing Selenium with Python3 to automate the process of entering data into a specific website. Despite my efforts to search for solutions on how to select an element by class when there are multiple options available, I have not been s ...

Extracting data from SVG tags within a funnel visualization

I encountered an issue while attempting to scrape data from a specific source. The code I initially used was obtained from this Scraping using Selenium and python Although the code worked flawlessly in the past, it has recently started throwing errors. ...

Using Selenium with Python to log in via a pop-up interface

Currently, I am attempting to log into a certain website using Python 3's selenium webdriver. To start the login process, I first have to click on the "Inloggen" button. Following that, I need to enter my username and password before clicking on the n ...

Having trouble performing mouse over actions with Selenium and Firefox?

Is there a solution for the mouse hover over an element in Firefox issue with the org.openqa.selenium.UnsupportedCommandException error? Error message: org.openqa.selenium.UnsupportedCommandException: mouseMoveTo Build info: version: '3.4.0', re ...

Locating and identifying visible elements on a web application with Selenium

I am struggling to locate visible elements on a web application page using Selenium. Specifically, I need to identify a clickable row and then click on it. The challenge lies in the fact that these elements are dynamically generated and do not appear in ...

Utilizing Selenium to scrape a table and displaying the data as a single long line instead of formatted columns and rows

I am currently working on web scraping from a particular website, and this is what my code looks like so far: import click from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from bs4 import BeautifulSoup table_rows = [] ...

A guide on extracting data from shopee.co.id

Having trouble finding the element for both username and password on shopee.co.id. Can someone offer some assistance? I'm trying to learn how to scrape data from the web, but I can't figure out how to instruct Python to input my email and passwor ...

Discovering elements based on text content instead of tags can be achieved by employing

HTML <div class="cell"> <!----> message. <!----></div> Attempting to create an XPath using text and then click on the specified element. My locator (//td[@class="el-table_1_column_5 el-table__cell"]//div[contai ...

Testing the functionality of an Android application through the use of the Selenium

I'm interested in testing an Android app using Selenium WebDriver. The challenge is that I only have the APK file and not the source code. Question: Can I record test steps and run them using Selenium WebDriver without access to the source code? Is ...

Is it true that using selenium driver.get() is considered automated and devoid of human influence?

Currently, I am working on a web scraping project where the program needs to scrape a shop that spans across multiple pages. I have found it challenging to simulate the correct actions for clicking certain elements, so instead I have been using Beautifulso ...

Guide to taking a screenshot of each 'li' element sequentially using a for loop

I am facing an issue with capturing screenshots of individual items from a list. The code I have written currently captures the entire page screenshot instead of just one item at a time. I also need it to scroll when the remaining "li" elements are hidde ...

Error: Unable to run file selector in Selenium RC

Struggling to automate testing on a 3rd-party site using selenium rc, I encountered an issue with the file chooser. Upon further investigation, I discovered it was related to the browser's native file chooser. Although I managed to handle that problem ...

Unable to enter password in Selenium password input field

I am encountering an issue where I can input my username on a website, but the password field does not allow me to enter any text. Even after waiting for the field to become clickable, it still does not work as expected. from selenium.webdriver.common.by i ...

Facing a stale exception error when dealing with dropdowns in Selenium using Java

I have a test case where I perform the following steps: 1. Open the main page 2. Click on a link that opens a new page/tab 3. Query data (Click on Query button. Page gets reloaded) 4. Select a drop down – “request”, fill in some text data, and click ...