Obtaining locator information from a WebElement and implementing it with the PageFactory

When designing my web application, I adhere to the Page object model by utilizing a Base class that contains all common and reusable methods, as well as separate pages for each page of the application.

Currently, I am faced with the task of creating a method in the BasePage that can be utilized in other pages. In each page, I have employed page factory to handle the elements.

One particular challenge I am encountering involves extracting locators into the method below so that it can be used across multiple pages with different element locators:

BasePage method:

    protected boolean CheckSorting(List<WebElement> element) {
        List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.)); // struggling here
        LinkedList<String> issueTypes = new LinkedList<String>();
        for (int i = 0; i < issueTypeDropdown.size(); i++) {
//            System.out.println(issueTypeDropdown.get(i).getText());
            issueTypes.add(issueTypeDropdown.get(i).getText());
        }
        return Compare(issueTypes);
    }

I am facing difficulty with this line of code:

List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.)); // stuck here

The PageFactory setup in the pages where I implement it is as follows:

public @FindBy(xpath = "(//INPUT[@type='search'])[4]/following-sibling::UL") List<WebElement> List;

Answer №1

In my previous experiences, I have encountered a similar issue that required me to find an alternative solution. Looking at the WebElement interface definition, I noticed that there is no method available to retrieve the "By" locator used to locate the element.

Fortunately, this question contains some answers that may help resolve your problem: Retrieving the By locator of an already found WebElement

One approach could involve using a custom class that implements WebElement and stores references to both the By locator and the Webelement separately.

Based on my past experiences, understanding how the page factory functions can be beneficial in addressing these issues. Consider creating your own View Factory to customize as needed.

Although it may not be a perfect solution, sometimes avoiding the use of the page factory altogether or developing a custom solution is the only way to achieve desired functionalities.

Best regards!

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

Automate the process of activating "Preserve log" in Chrome using Chromedriver

Is there a way to programmatically enable the "preserve log" option in Chrome Developer Tools settings? This can be done through chromeoptions.add_argument, adding the pref to DesiredCapabilities, or any other method? ...

Encountering an issue with WebDriver in the realm of JavaScript

I am struggling to use JavaScript to locate specific controls and send values to them. One example is changing the text in a textbox with the ID "ID" to "123456". Below is the code I tried: ((IJavaScriptExecutor)driver).ExecuteScript("document.getElement ...

Extract dropdown menu options

OBJECTIVE The aim is to explore every possible combination from this website: WHAT I'M SEEKING ADVICE ON I need some guidance on how to proceed with the task. ISSUE AT HAND I am facing a problem where I can retrieve a list of options for the first ...

What is the process for establishing a class within PageObject framework?

As a beginner in Selenium, I am using IntelliJ along with Selenium WebDriver and Junit. My current challenge lies in setting up the TestBase class within the PageObject framework. Below is an excerpt of my TestBase class: import org.junit.After; import or ...

I'm encountering a webdriver issue in my IntelliJ IDE when trying to run Selenium tests

While working on my Selenium project, I encountered an error in IntelliJ IDE that did not occur when using Eclipse IDE. Attach is a snapshot showcasing the issue. The code runs without any issues in Eclipse, and I have already included the necessary depend ...

Updating the month involves the process of parsing dates

Here's the code I'm using to parse a date in the format "DD/MM/YYYY" for INTAKE_DATE, which is currently set to "2015-04-20": String INTAKE_DATE = rs.getString("INTAKE_DATE"); //"2015-04-20" System.out.println(INTAKE_DATE); java.text.DateFormat ...

Ensuring a User has an Image in MySQL Using Angular 6

As part of my development process, I am working on creating a new user and sending their information along with an image to a MySQL database. The process involves sending a user object with form data through the following component.ts file: subscribeUser() ...

Initiate the process of displaying data on a datetime chart using Highcharts

I am currently developing a yearly chart, but I've encountered a small issue. The chart begins in January, however there is no data available until May. The client specifically wants the chart to only display when there is data available, and unfortu ...

Utilizing Selenium, Kotlin, and PageFactory to instantiate a collection of web elements

I've been experimenting with PageFactory and Selenium, but I'm encountering difficulties when dealing with lists of elements. Here's the code snippet causing me trouble: @FindBy(css = "timeline-instances [data-e2e-selector=inst]") ...

Mastering the use of getText() in Protractor with Page Object Model in Javascript

Having trouble retrieving specific values from my page object. The getText() method is returning the entire object instead of just the text, likely due to it being a Promise. I can provide my code if necessary, but I'm aiming to achieve something sim ...

Scraping social media followers using web scraping, however, the list is massive with hundreds of thousands. Selenium crashes due to memory overload

After using Selenium in Chrome to gather usernames from a social media profile, I encountered an issue with the limited loading of the page and Chrome crashing due to running out of memory. The list of followers is extensive, reaching hundreds of thousands ...

What is the best way to retrieve the php $_SESSION variable within Mink (Behat) framework?

My current challenge involves writing code to log in a user, which is necessary for the scenario. I want to streamline this process by avoiding the need for the browser to actually visit the login page, fill in fields, and submit the form repeatedly. Inste ...

Using Python with Selenium and encountering the stale element reference issue

As I navigate through a webpage, I am attempting to store a collection of links on the page that I want to click on, and then I plan to click on each link within a loop. Here is the code snippet I have written: from selenium import webdriver driver = web ...

What is the process of sending JSON parameters in an Android web service request?

Possible Duplicate: How to send a JSON object over Request with Android? Being new to Android development, I encountered an issue when trying to send requests to a web service in JSON format. After researching online, I came across this code snippet f ...

What sets apart the usage of pollingEvery when waiting for an element with Selenium?

Consider the following scenario: FluentWait fluentWait = new FluentWait[WebDriver](driver) .withTimeout(timeOut, TimeUnit.SECONDS) .pollingEvery(10, TimeUnit.SECONDS) fluentWait(120).until(ExpectedConditions.elementToBeClickable(element)) Ca ...

How can I determine the specific quantity of XPATH links with unique identifiers in Selenium?

Seeking automation with Python3 and selenium to streamline searches on a public information site. The process involves entering a person's name, selecting the desired spelling (with or without accents), navigating through a list of lawsuits, and acces ...

Angular4 ChromeDriver Selenium Protractor

I am facing an issue while trying to run 'ng e2e'. The error message I encounter is as follows: WebDriverError: unknown error: cannot find Chrome binary I am using Protractor which is pre-installed with Angular CLI. Despite reinstalling ChromeD ...

Experiencing difficulties with ng select elements when performing selenium tests

Currently, I am facing a challenge in testing a specific element on a website that utilizes ionic for its front-end implementation. This particular element involves a listbox using ng-select. Despite my efforts to explore various solutions and experiment ...

Having trouble with clicking the Sign-in button

<div class="form-group dl-padding-10"> <select class="form-control form-control-solid" name="SelectedRoleID" id="SelectedRoleID" onchange="removeBorderColor()" required=&qu ...

What is the most effective way to gauge the extent of automation coverage on a system being tested through analytical reports

What are some effective methods for quantifying automation statistics and coverage for a system being tested based on your experience? Summary: Presently, there are more than 100 tests (in feature files & scenarios) categorized by the area of the system ...