0

The HTML code is

<div class="_4w5j">
    <strong>
        <span>username</span>
    </strong>
    <span class="_-kj">—</span>
    <span class="_4w5k">
        <i class="img sp_4YfKd3lpYjo sx_5f57b1">
            <u>5 star</u>
        </i>
    </span>
    <span>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span>
</div>
  1. I need the text "username" inside strong/spam It is works for me the code is

x= i.find_elements_by_xpath('//div[@class="_4w5j"]/strong')

for d in x:
    print d.text

It's print the correct result

I need the text of 'x'inside the span[3] 'xxxxxxxxxxxxxxx' the below code also works good.

Question:

I need the text inside the u tag '5 star'. I used the below code but it prints the empty result.

y= i.find_elements_by_xpath('//div[@class="_4w5j"]/descendant::u')
for d in y:
    print d.text

I tried this also

y= i.find_elements_by_xpath('//div[@class="_4w5j"]/span[2]/i/u')
for d in y:
    print d.text

Not working for me. Please help me . Thanks in advance

<a class="uiLinkSubtle" href="/sherryl.cheary/activity/10152833559161998">
<abbr class="timestamp" data-utime="1419713801" title="Saturday, December 27, 2014 at 12:56pm">about a month ago</abbr>
</a>

I need the text inside the title but the below code sh

z= i.find_elements_by_xpath('//a/descendant::abbr/@title')
for dd in z:
    print dd.text

Its shows the error called " web element has no attr title or some thing wrong in xpath"

12
  • can u try with xpath: //span class='_4w5k' and then do element.text? Commented Feb 5, 2015 at 7:05
  • and for second part can you do use xpath: //abbr[@class='timestamp'] and then call get_attribute("title"). I have never worked in python, so please check the signature of get_attribute method. Thanks Commented Feb 5, 2015 at 7:08
  • the xpath shows Error //span class='_4w5k' is either invalid or does not result in a WebElement Commented Feb 5, 2015 at 7:18
  • sorry my mistake "//span[@class='_4w5k']". Just forgot to put square brackets. Commented Feb 5, 2015 at 7:19
  • use css selectors, xpaths are not stable at all. To find <u elem you can use next selector : "._4w5j > i > u" Commented Feb 5, 2015 at 7:19

2 Answers 2

0
i.find_elements_by_xpath('//div[@class="_4w5j"]/span//u')[0].text

i.find_elements_by_xpath('//a/abbr')[0].get_attribute('title')
Sign up to request clarification or add additional context in comments.

5 Comments

For first one it print emptly only sir, For abbr it prints IndexError: list index out of range
No problem with text inside abbr but i need text inside the u tag
My typos. Try again pls.
I was confused by your random class names and now I edit again.
Anyway, my main points are 1. use // instead of descendant 2. attribute isn't an element
0

Why do you stick with xpath?

Consider this

 find_element_by_tag_name('u').text         

 find_element_by_tag_name('abbr').get_attribute('title')

You have to adjust it to search for elements if you have multiple u and abbr tags.

Anyway this will be much more readable for you in future.

In may opinion you should not overuse xpath stuff, especially if you can get what you need with simpler methods like lookup by tag_name or class or css_selector

3 Comments

Yes I under stand the concept but why the find_element_by_tag_name('u').text show only the empty string new line.
only problem with a text inside u tag
FYI these all are child path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.