Choosing a specific text within a webpage using Selenium

<aside class="reply-flap js-captcha">
<ul>

        <li>
            <h1>contact name:</h1>
            <p>joel Rupp</p>
        </li>
    <li class="reply-tel">
        <h1>
                <a class="reply-tel-link" href="tel:14085551324">call</a>
                    <span class="reply-tel-text">call</span>

or
                <a class="reply-tel-link" href="sms:14055651324">text</a>
                <span class="reply-tel-text">text:</span>
        </h1>
        <p class="reply-tel-number">
            ☎ (408) 555-5678
        </p>
    </li>

    <li class="reply-email">
        <h1>reply by email:</h1>
            </li>
    <li>
        <h1>webmail links:</h1>
        <ul class="reply-emails">
            <li>
                <p>
                          </p>
            </li>
            <li>
                <p>
                           </p>
            </li>
            <li>
                <p>
                           </p>
            </li>
            <li>
                <p>
                           </p>
            </li>
        </ul>
    </li>
    <li>
        <h1>copy and paste into your email:</h1>
        <p class="anonemail"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbfb6bbb1a6f1eae8e4e8efefebeafbfba98b9acfafbaba3e48499">[email protected]</a></p>
    </li>
</ul>

</aside>

I need to extract the contact name from this HTML code. However, sometimes the contact name is empty and in those cases, I get the email address instead. In such instances, the output should be blank when the name is not found. I am currently using the following code:

name = driver.switchTo().window(child_window).findElement(By.cssSelector("aside.reply-flap.js-captcha ul  > li:nth-child(1) p")).getText();

I am aware that if I were using Jsoup, I could use "contains(contact name:)". How can I achieve a similar functionality in Selenium?

I have tried something like

h1[contains(text()='contact name:')]

but it is not working at all.

Answer №1

If you need to extract the text enclosed in the <p> tag right under the <h1> tag containing the phrase contact name while using contains, here is an example of how to achieve it with xpath:

name = driver.switchTo().window(child_window).findElement(By.xpath("//aside[@class='reply-flap js-captcha']/ul/li/h1[contains(text(),'contact name:')]//following::p[1]")).getText();

Note: The use of :contains as a CSS pseudo-class does not comply with the specification. For more information, refer to this insightful discussion on related exceptions and solutions: selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

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

Having trouble clicking on a class and image with Selenium

I'm running into an issue with Selenium web driver where I cannot click on the search image. I've tried using XPath as well as Selenium IDE, but nothing seems to work. Here's the code snippet using XPath: driver.findElement(By.xpath("//for ...

Rowing and Columning with Bootstrap

Hey there! I'm currently working on creating some stylish boxes to showcase text and possibly an image background. However, I'm encountering an issue where the boxes are not aligning correctly on smaller screens. https://i.sstatic.net/JoYtj.jpg ...

The text beside the radio button appears slightly elevated, rather than perfectly aligned next to it

I'm currently working on HTML radio buttons and I have a slight issue with the alignment of the text near them. The radio buttons are placed correctly, but the text appears to be slightly lifted. Can someone please help me align the text properly next ...

Adjust the size of the title font and modify the background colors

One of my titles looks like this: <a title="I am your title"/> The default background color is yellow Take a look at this image which can help illustrate what I'm asking about Is there any way to change the background color and increase the ...

Guide on re-running failed tests using the jvm-cucumber-parallel-plugin

I am looking to utilize the jvm cucumber parallel plugin in order to re-run any failed test cases. What adjustments need to be made in the .pom file? <plugin> <groupId>com.github.temyers</groupId> <artifact ...

Guide to setting up a multi-column grid system with Bootstrap and CSS

Trying to create a grid layout using Bootstrap, I need some assistance. Check out the image for reference: enter image description here Here's the code I've been working on, but I can't seem to get it right: <section id="section-2&q ...

Several DIVs with the same class can have varying CSS values

I am looking to modify the left-margin value of various separate DIVs using JavaScript. The challenge is: I only want to use a single className, and I want the margin to increase by 100px for each instance of the class. This way, instead of all the DIVs ...

What could be causing Safari to alter the heights of my div element?

After completing a challenge for FrontEnd Mentor, I excitedly texted a friend my demo link to showcase my work. However, upon opening the link on my phone, I discovered that the formatting was all over the place. Switching to Safari on my desktop only made ...

Selenium IDE: Triggering an Ajax request on play button activation

Is it possible to trigger an Ajax call using jQuery $.ajax function in Selenium IDE when the user clicks the play button? I haven't been able to find any information on how to accomplish this after searching online for 20 minutes. ...

Expanding list selection with jQuery_hover effect

I have devised the following code. HTML <ul class="treatment-menu"> <li>Menu always visible</li> <ul class="nav-child"> <li>Hidden sub menu</li> </ul> </ul> Hover function $(&a ...

Maintain the color of a Bootstrap 4 dropdown link even when it is open by incorporating hover

Currently, I am utilizing bootstrap 4 to create a menu that includes dropdown items. The issue I am facing is that when hovering over a dropdown link, the background color changes and the dropdown opens. However, as soon as I move the mouse to select one o ...

Arranging CSS text in a parallel format and aligning it vertically

I am working on a layout with two columns of text placed side by side, where one column is right aligned and the other is left aligned. Below is my current code: .alignleft { float: left; } .alignright { float: right; } .list-unstyled { padding ...

The Enchanted URL Folder Name

Spent two painstaking hours dealing with this issue. It's incredibly frustrating. Struggling to load css files in PHP pages when the URL contains a folder named "adsq" Comparing two identical pages, only differing in the folder name: One works perf ...

Having trouble starting any browser using selenium webdriver

I've been attempting to utilize Eclipse for running Selenium web-driver automation, but I'm facing an issue where the browser window won't open. Despite checking the debug logs and finding no errors, I am able to successfully retrieve the ti ...

hiding certain values during output in java

Hey there! I ran into an issue where my console was printing null values while trying to retrieve all product_ids on the category page. I only want to display exceptions for those null values, so here's the snippet of code that I'm working with: ...

Is it possible to alter the style of the <input type="file"> element?

After attempting to modify the HTML form input type file, here is the snippet of code I used: In HTML, within form id = form <input class="upload_file" type="file" id="file" name="file" /> .CSS I experimented with both approaches: .upload_button{ ...

Protractor experiencing sluggish performance on Internet Explorer11

When using Protractor to run my test cases in Internet Explorer 11, I noticed that it takes significantly longer compared to Firefox and Chrome. It even takes around 20 seconds just to enter the text "Hello" into a text box. Is there a way to improve the ...

In React, I'm unable to navigate to a different page

My English may not be the best, but I am working on creating a Login Page. The issue I'm facing is that when I click the Login button, I want to navigate to the Home Page I designed using React. However, whenever I try to implement Link or Route comma ...

Navigating websites: Clicking buttons and analyzing content

When looking to navigate a website's directory by utilizing buttons on the page or calling the associated functions directly, what language or environment is most suitable for this task? After experimenting with Python, Java, Selenium, and JavaScript, ...

Spacing between hidden checkboxes and their labels

I've customized some checkboxes to resemble a select dropdown by hiding the input and using only the label as the visible element. However, I'm struggling to add margin between the elements, which seems to work only when the checkbox is visible. ...