From converting a path from a string to a WebElement

Currently, I have implemented the following method in my BasePage and I am looking to utilize this method in other pages as well.

In this method, the parameter is currently set as (String xpathExpression). How can I modify it to accept a WebElement instead and make use of different element locators defined in other pages?

protected boolean CheckSorting(String xpathExpression) {
    List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.xpath(xpathExpression)));
    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);
}

Answer №1

If you are unable to retrieve the locator from a WebElement, one solution is to make the locator strategy dynamic by passing a By parameter to the method.

protected boolean VerifySorting(By by) {
    List<WebElement> dropdownElements = new LinkedList<>(driver.findElements(by));
    //...
}

Example of how to use this method:

VerifySorting(By.xpath(xpathExpression));

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

Sending POST parameters via Jquery POST from a Java Servlet to Javascript

My JavaScript code initiates a POST request on my Servlet. $.post("RecipeServlet", { r_id: r_id, }, function(data, status){ var foo = data.foo; var bar = data.bar; }); Now, the Servlet is expected to work with the received r_id and then send ...

Receiving an error message when sending text input inside an iFrame Selenium due to discarded browsing context

As I attempt to input my username on a website, I realize it is situated within an iframe on the page. The HTML code for the iframe and input field appear as: <iframe class="_2_i9tg _2_i9tg" src="https://gateway-members.bet365.com/v1/auth ...

The spring controller sends back an empty Ajax response, despite successfully completing the request

The data received from the Spring controller appears to be empty when passed to the AJAX success function. To troubleshoot, I attempted returning a string directly from the controller like so: @ResponseBody @RequestMapping(value = "/test", method = Reque ...

Having difficulties executing Selenium with htmlSuite on a headless Linux environment

After building a basic test using Selenium IDE and converting it into an HTML suite, I set up a VM running CentOS 5.5 to run my Selenium tests. However, when attempting to execute the following command: java -jar selenium-server-standalone-2.14.0.jar -mul ...

Maintain only one instance of WebDriver in C# programming

I am currently working with NUnit 3 and Selenium 3 in VB2015. My project setup involves having a driver creation class, a base class for setup and teardown operations, and separate test classes for different functionalities like clients, invoicing, and est ...

Is there a way to transform this nested json string into a Java object?

Help needed with converting a JSON string in a unique "special" format into a Java object. Struggling to access individual values such as the location or "resolved_at". Tried using GSON and JSONPOBJECT without success. { "result": { "upon_approval ...

Attempting to parse a website with selenium and bs4 fails to yield any results

I'm attempting to scrape data from the following website: driver = webdriver.Chrome() # brew install chromedirver driver.get(self._SCRAPE_WEBSITE_URL) page = driver.page_source soup = BeautifulSoup(page, 'lxml') cve = ...

Selenium error: Element not found within iframe troubleshooting

When attempting to run a selenium script for automated testing on servicenow, I encountered an issue where an element was not found when trying to populate a field within the webpage. The login page contains an iframe, but after logging in, it seems that t ...

Unable to Configure SOCKS Proxy with Firefox Webdriver

I am facing an issue with setting up a SOCKS5 proxy in Java using the Firefox driver. final FirefoxOptions FIREFOX_OPTIONS = new FirefoxOptions(); final Proxy PROXY = new Proxy().setProxyType(Proxy.ProxyType.MANUAL).setSocksVersion(5).setSocksProx ...

Selenium comprises an instance of Mozilla Firefox

I am in the process of setting up a server where I need to create a script that can log in to a page that uses JavaScript. To accomplish this, I plan to implement Python Selenium. Our shared drive houses all the necessary binaries, which must be integrate ...

Extracting Form Data using Selenium

Fields in form data of an HTTP request are typically used for forms, but I recently discovered they can also be used when communicating with a REST server. Is it possible to include form data in a Selenium HTTP request? As shown in the network communicatio ...

Interacting with Selenium API using a jQuery event handler

I am working on a Java Selenium application that needs to respond to specific events occurring in an open browser window. One such event is when a user interacts with elements on the page, and I need my Java application to be aware of it so that it can tak ...

What steps do I need to follow to utilize selenium in headless mode for web scraping on this specific

Attempting to scrape information from this website () using selenium on Ubuntu within a docker container has presented challenges. Specifically, when trying to utilize chromedriver in headless mode, the script fails to extract the desired data. Interestin ...

Exploring with Cucumber and Selenium: is there a way to view the browser's perspective?

While working on a scenario in Rails with Cucumber, I encountered an issue where it couldn't locate the edit link to click on. When I manually checked the page, the link was there. I know I must be missing something, but I can't pinpoint what it ...

Request for Selenium element

I'm struggling to locate an element using the Selenium library. I've attempted to find it by id, by Xpath, but so far, no luck... I suspect the issue lies within the HTML iframe or framesets, despite also trying driver.switch_to.frame without any ...

Retrieving stored information from the browser when making a jQuery AJAX request

I've got this AJAX call written in jQuery that continuously receives JSON responses from the controller and displays them on the screen by setting values (Code to set values is not included below). $(function() { $(".master_status").hide(); setIn ...

Python WebDriver in Selenium: Using Conditional Statements for Element Selection

In my current situation, I am dealing with a modal that may or may not have a Facebook login button directly available. Sometimes I need to click on 'More Options' to access the Facebook button. The challenge here is that the xpath for the more_o ...

Issue with accessing data from database in my app

I am currently working on a project that involves retrieving data from my database. The user inputs a car registration number and a rating (ranging from 1 to 5) and then clicks a button. Upon clicking the button, my code is supposed to execute, fetching te ...

Utilizing Selenium and Java to Alter the Source Code of a Web Page

I am dealing with a page that has source code structured like this: <header style="position: fixed;"> Unfortunately, when I try to use my Java script, clicking on an element becomes impossible because it is located underneath the header. This resul ...

I encountered a webdriver error while using Selenium in Google Colab

I attempted to find the solutions through Google, however, they proved to be ineffective. I am feeling quite disheartened at the moment. ...