Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:05:23+00:00 2026-06-17T07:05:23+00:00

Assume we have the following html: <html> <body> <a href="/1234.html">TEXT A</a> <a href="/3243.html">TEXT B</a>

  • 0

Assume we have the following html:

<html>
    <body>
        <a href="/1234.html">TEXT A</a>
        <a href="/3243.html">TEXT B</a>
        <a href="/7445.html">TEXT C</a>
    <body>
</html>

How do I make it find the element "a", which contains "TEXT A"?

So far I’ve got:

root = lxml.html.document_fromstring(the_html_above)
e = root.find('.//a')

I’ve tried:

e = root.find('.//a[@text="TEXT A"]')

but that didn’t work, as the "a" tags have no attribute "text".

Is there any way I can solve this in a similar fashion to what I’ve tried?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:05:24+00:00Added an answer on June 17, 2026 at 7:05 am

    You are very close. Use text()= rather than @text (which indicates an attribute).

    e = root.xpath('.//a[text()="TEXT A"]')
    

    Or, if you know only that the text contains "TEXT A",

    e = root.xpath('.//a[contains(text(),"TEXT A")]')
    

    Or, if you know only that text starts with "TEXT A",

    e = root.xpath('.//a[starts-with(text(),"TEXT A")]')
    

    See the docs for more on the available string functions.


    For example,

    import lxml.html as LH
    
    text = '''\
    <html>
        <body>
            <a href="/1234.html">TEXT A</a>
            <a href="/3243.html">TEXT B</a>
            <a href="/7445.html">TEXT C</a>
        <body>
    </html>'''
    
    root = LH.fromstring(text)
    e = root.xpath('.//a[text()="TEXT A"]')
    print(e)
    

    yields

    [<Element a at 0xb746d2cc>]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have the following html: <html> <head> </head> <body> <div id=wrapper > <div
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
Ilets assume i have the following HTML <a class='x y'>A</a> <a class='x '>B</a> <a
I am trying to solve the following problem. Assume I have a HTML file
Assume following html page: <html> <head></head> <body> <div id=wrapper> <div id=header/> <div id=navigation/> <div
I have the following html-displayed question: <div id=Question1><span>1. </span>Which of the following have the
I have a pretty simple scenario. I have the following HTML: <h1>Hello</h1> <input type="button"
I have to following HTML: <ul class=ulclass> <li class=liclass> ...some more nesting <a href=somelink>somelink</a>
Assume I have the following HTML - <DIV id=ParentDiv> <DIV id=SubDiv1></DIV> <DIV id=SubDiv2> <INPUT
I have the following HTML code <div class=text>bla bla bla bla</div> <div class=button>Show</div> And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.