Locators like ID, class, and XPath are frequently used to interact with web items in Selenium-based web automation. However, there are situations when utilizing text content might be the simplest approach to find an element, particularly when other qualities are not distinctive or easily accessible. While jQuery has a built-in: contains CSS selector, which Selenium lacks, we may use it to improve our Selenium scripts in Java and Python.
With this article, we’ll look at how to use the jQuery’s`: contains` selector with Java and Python Selenium WebDriver scripts.
jQuery with Selenium as the Foundation
jQuery integration with Selenium
The Injection Mechanism
You can dynamically create a script element, give it the jQuery URL, and add it to the header of a web page to inject jQuery into it. This makes it possible for the website to use all of jQuery’s features even if jQuery wasn’t originally there.
Version Compatibility: Make sure the jQuery version you inject is appropriate for the web page and does not interfere with any of its features.
Avoiding Conflicts: jQuery should be injected in a way that prevents conflicts with any JavaScript libraries that are already present on the web page.
Load Time : Before running any jQuery-based scripts, ensure that jQuery has fully loaded. putting wait techniques into practice or verifying jQuery.is Ready can guarantee that jQuery has completed initialization.
jQuery validation
To avoid reference issues in our scripts utilizing jQuery, it is crucial to verify that jQuery is either available or has been correctly injected into the web page before employing jQuery selectors.
Specific Case Applicability
Although jQuery offers a wide set of features, it’s important to assess each situation individually to decide whether using jQuery is appropriate. Due to their simplicity and direct contact with the browser’s API, native Selenium locators may be used for straightforward interactions.
Utilizing jQuery :contains Selector
Inject jQuery: Verify if the selectors are available on the web page before using them. Inject it if it isn’t present.
Locate Element: Use Selenium’s Javascript Executor to run a jQuery script using the: contains selector.
Interact & Validate: Engage with the element, perform actions, and validate outcomes using assertions.
Java Sample Code for Selenium Setup and Execution
Here is an example Java code for Selenium WebDriver that uses jQuery’s: a text selection that may be used to interact with a web element
import org.openqa.selenium.*;
|
Explanation
Before using jQuery selectors, we ensure jQuery is available on the webpage using isJQueryLoaded(). If not, it gets injected using injectJQuery().
if (!isJQueryLoaded(driver)) { injectJQuery(driver); } |
We execute a jQuery script to locate the button containing the text “Double Click Me” and retrieve its text content.
String elementText = (String) js.executeScript( “return $(‘button:contains(Double Click Me)’).first().text();” ); |
Lastly, we assert to ensure the fetched text matches our expectation, ensuring our selector works as intended.
Assert.assertEquals(elementText, “Double Click Me”); |
Setup and Execution in Selenium with Python
Here’s how you might achieve similar functionality using Selenium WebDriver with Python:
from selenium import webdriver
|
Explanation
The Python implementation follows a similar logic:
WhatsApp us