1

I'm trying to get the text from the span inside this div with python and selenium:

<div class="product-name">
    <span class="h1" itemprop="name">TEXT</span>
</div>

I've tried this, however, this returns an empty string:

line = dr.find_element_by_class_name('product-name').find_element_by_xpath('.//span').text

Thanks in advance,

1
  • you could simplify the search like this: line = dr.find_element_by_xpath('//div[@class="product-name"]/span').text. If it still doesn't work, it may be a synchronization issue (wait for element instead of searching for it) Commented Oct 8, 2016 at 22:19

2 Answers 2

4

You should try using css_selector to find desire element in one find statement as below :-

line = dr.find_element_by_css_selector('div.product-name > span').text

If you're still getting empty string try using get_attribute("textContent") as :-

line = dr.find_element_by_css_selector('div.product-name > span').get_attribute("textContent")

Or using get_attribute("innerHTML") as :-

line = dr.find_element_by_css_selector('div.product-name > span').get_attribute("innerHTML")

Note :- You can also use above operation to getting innerText on parent <div> element using class_name if there is only desire text as :-

line = dr.find_element_by_class_name('product-name').text

Or

line = dr.find_element_by_class_name('product-name').get_attribute("textContent")
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I've used css selector and it works, but I'm just curious to see why my approach does not work.
It's hard to say why your approach is not working, your approach also is absolutely correct, I think there are more element with same class name and it's locating other span instead of desire. Thanks..
0

I find bs4 more intuitive, prehaps this would suite better?

    from bs4 import BeautifulSoup as bs4


def main():
    html = """<div class="product-name">
            <span class="h1" itemprop="name">TEXT</span>
            </div>"""
    soup = bs4(html, "html.parser")
    print(soup.find_all('div', {"class": "product-name"}))


if __name__ == '__main__':
    main()

Concerning your code..

line = dr.find_element_by_class_name('product-name').find_element_by_xpath('.//span').text

Maybe supposed to be something more inline with:

line = dr.find_element_by_classname('product-name')

Might remember wrong.

Comments

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.