In search of a CSS selector that can target elements based on specific text contained within them

Query:

<div class="btn btn-second-in-pair-not-desired btn-tall">Clear search</div>
<div class="btn btn-second-in-pair-not-desired btn-tall">Raw Search</div>
<div class="btn btn-second-in-pair-not-desired btn-tall">Basic Search</div>
<div class="btn btn-primary-stand-alone btn-tall search-btn">Search</div>

This is the approach I've attempted so far - ".btn:contains('Clear search')" but selenium fails to locate it

Answer №1

The CSS Selector no longer recognizes the :contains() function. In order to achieve the same result, you must utilize XPath with the expression "//div[text()='Clear search']".

Answer №2

Check out the following CSS code:

css=div:contains('Clear search')

For more information, visit this link: SauceLab

Answer №3

When working with Selenium, XPATH strings are also accepted as a more flexible alternative to CSS Selectors in certain scenarios. Personally, I appreciate the convenience of being able to simply right click on a tag within the Elements view of DevTools and select "Copy > Copy XPath."

webDriver.findElement(
  By.xpath(
    "//div[text()='Clear search']"
  )
).click();

Answer №4

Is it possible to modify the class based on the content?

<?php 
$content = $your_content_from_database;
$arr_content = explode(" ", $content);
$first = $arr_content[0];
echo '<div class="btn btn-second-in-pair-not-desired btn-tall clear" data-content="'.$first.'">'. $content. '</div>' ; ?>

Here is the CSS

[data-content="Clear"]{
color:red;
}

Answer №5

Based on the information provided in your question, you can use the following CSS selector to identify elements:

// select all elements inside the list for a div with the same class
List<WebElement> mycsselements =  driver.findElements(By.cssSelector(".btn.btn-second-in-pair-not-desired.btn-tall"));
System.out.println("Size of the div elements with the same class name : " + mycsselements.size());
// print values one by one 
System.out.println("First value is "  + mycsselements.get(0).getText());
System.out.println("Second value is " + mycsselements.get(1).getText());
System.out.println("Third value is "  + mycsselements.get(2).getText());

// for the last one, do it like this

System.out.println("Last value is " + driver.findElement(By.cssSelector(".btn.btn-primary-stand-alone.btn-tall.search-btn")).getText());

Here is the output:

Size of the div elements with the same class name : 3
First value is Clear search
Second value is Raw Search
Third value is Basic Search
Last value is Search

I hope this information is helpful for you.

Answer №6

It appears that css does not have the capability to search by the content of an html element.

One possible solution could be:

<div class="btn btn-second-in-pair-not-desired btn-tall clear">Clear search</div>
<div class="btn btn-second-in-pair-not-desired btn-tall raw">Raw Search</div>
<div class="btn btn-second-in-pair-not-desired btn-tall basic">Basic Search</div>
<div class="btn btn-primary-stand-alone btn-tall search-btn">Search</div>

You can then style these elements using classes:

.btn{
  color:red;
}

.clear, .raw, .basic, .search-btn{
  color:blue;
}

Alternatively, you may consider using ids:

<div id="search">Search</div>

And target it like this:

#search{
 ...
}

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

Implementing a dynamic loading strategy for Google reCAPTCHA based on language selection

I have a unique application that requires the selection of one language out of four options (English, French, Dutch, español) in a form. Below the language selection, the Google reCaptcha is displayed. I am looking to dynamically load the reCaptcha scrip ...

Python script for logging in using Facebook with Selenium

I've been working on a selenium script to automate logging in with Facebook for Spotify online. The page layout is shown below. The login link is nested within 2 divs. I've attempted to locate it using class names or the text "Log in with Facebo ...

Creating uniform-sized flex items with varying image sizes

When setting flex: 1 1 0 on flex items, it does not work as expected especially when the flex items contain images of varying sizes. In this scenario, the flex-item containing the image ends up becoming significantly wider than the others. Below is the H ...

selenium executes the method multiple times consecutively

Recently, I have begun working with Selenium. The structure of my test is as follows: Using the @BeforeClass annotation, I create an object and call a method from a class that opens the browser and performs a login operation. I have another method which ...

Using Selenium class annotations in Java for Page Objects

Can you assist me with utilizing class(page) annotations in the Page Object pattern? For instance, I have a "Contacts" WebElement in both the upper menu and footer sections, using the same locator. I am aware that I can annotate Footer.class and UpperMenu. ...

Difficulty aligning headings in HTML

I'm puzzled by a strange 1px gap on the left of my h2 heading, which sits atop an h3. The font sizes are set to 40px for h2 and 12px for h3. Can anyone help me solve this mystery? Any assistance would be greatly appreciated! Thank you body { pa ...

Is it possible to run a Universal Windows Platform desktop app on an Android mobile device?

After successfully developing a Universal Windows Platform app in Visual Studio using WinJS, JavaScript, CSS and HTML, I am now interested in leveraging the same code base to create an Android application. Could you guide me on the necessary steps to run ...

iOS 10's autofocus feature experiencing difficulties in focusing on input

While using an application on my desktop or Android device, I have noticed that the input focus works perfectly fine. However, when I try to run the same application on iOS 10 Safari, the input focus does not seem to be working. It is worth noting that I ...

bring in all the files located within the Directory

Is there a way to import all CSS files from a specific folder without having to import each file individually? Looking to import assets/css/* ? <link href="<?php echo base_url(); ?>assets/css/*" rel="stylesheet"/> <title&g ...

The checkbox is not updating as anticipated

I'm currently developing a basic widget that allows the user to change the value of either the check box OR the entire div by selecting them. Below is the code I have implemented: $(document).ready(function() { $(document).on("click", ".inputChec ...

Should we stick with reusing the same instance of the Page class in Selenium for multiple uses within the same test method, or is it better to create a fresh instance each time

To examine the functionality of the website at utilizing PageFactory design, I need to run a test. The steps in my test method are as follows: Step-1. Navigate to Rediff's home page Step-2. Click on the Sign-in button Step-3. Input the usern ...

ngx-datatable detail row failing to expand properly

I am striving to develop an ngx-datatable component that can be utilized universally for consistent styling and functionality. Although most features are working correctly, I'm struggling to understand why the details row isn't expanding as expe ...

Is Jquery struggling to modify the visual enhancements?

Check out this link, I need the sidebar design to be similar to what is shown in the provided link. Furthermore, I am looking for a way to have a pointer that follows along with my selection on different labels such as home, charts, etc. Can anyone assis ...

Limit selection choices in select element

Similar Question: Prevent select dropdown from opening in FireFox and Opera In my HTML file, I have a select tag that I want to use to open my own table when clicked. However, the window of the Select tag also opens, which is not desirable. Is there a ...

Issues arising from the interaction of one DIV with another DIV

I'm having trouble positioning a menu (height = 100%) next to the logo. Essentially, when the image controls the height of the DIV (container), it seems logical that adding another DIV (menu) with height: 100% inside that DIV (container) should resul ...

Enable the input field once the checkbox has been marked

Here is a checkbox example: <div class="form-group"> <input type="checkbox" id="examination" class="form-control" name="exam" placeholder="Enter Title"> <label>Enable Exam</label> </div> Additionally, I have a disabled inpu ...

textarea label align: center

I've been struggling to center-align the label for this textarea within the text box, but so far my attempts have failed. The resulting display resembles the following: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx ...

Is it possible to have Protractor utilize the IEDriverServer.exe that was updated by webdriver-update in the node_modules folder?

Currently, I am in the process of writing acceptance tests for my angular web application project. These tests are executed using protractor and everything is working smoothly on Chrome. However, when attempting to run the tests on Internet Explorer 11, an ...

What is the proper way to incorporate html entities in my specific situation?

In my application, I am attempting to incorporate double macron characters. My current method involves using the &#862; entity, like this: t&#862;t, which results in t͞t. However, I have encountered issues with reliability, as sometimes the ...

Harnessing the power of JSON within an HTML document to display dynamic data

Check out the JSON data through this link: The JSON file contains information about the weather. I need assistance with incorporating the data from the provided link into my HTML document as I am new to utilizing JSON for this purpose. <html> < ...