0
votes

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"

2
can u try with xpath: //span class='_4w5k' and then do element.text? - Vivek Singh
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 - Vivek Singh
the xpath shows Error //span class='_4w5k' is either invalid or does not result in a WebElement - AnandhaKumar Radhakrishnan
sorry my mistake "//span[@class='_4w5k']". Just forgot to put square brackets. - Vivek Singh
use css selectors, xpaths are not stable at all. To find <u elem you can use next selector : "._4w5j > i > u" - Andrey Egorov

2 Answers

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

i.find_elements_by_xpath('//a/abbr')[0].get_attribute('title')
0
votes

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