1
<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">cjgmz-6484327676@serv.ytutyuty.org</p>
    </li>
</ul>

</aside>

I want to select contact name from this code, though sometimes when the name is empty, i get the following (li) element text which is email address, it should output blank when name is not found. am using this code :

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

am aware that if i am using jsoup i can use contains(contact name:) how do i use contains in selenium ?

I have tried something like

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

and its not working at all

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To retrieve the text from the <p> tag just below the <h1> tag with text as contact name using contains you can use the following 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 :contains pseudo-class doesn't works as a CSS as per the Spec. Here you can find a detailed discussion selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352