How to find element value using Splinter?

2019-07-01 23:06发布

问题:

I have following piece of html:

<p class="attrs"><span>foo:</span> <strong>foo</strong></p>
<p class="attrs"><span>bar:</span> <strong>bar</strong></p>
<p class="attrs"><span>foo2:</span> <strong></strong></p>
<p class="attrs"><span>description:</span> <strong>description body</strong></p>
<p class="attrs"><span>another foo:</span> <strong>foooo</strong></p>

I would like to get description body using splinter. I've managed to get a list of p using

browser.find_by_css("p.attrs")

回答1:

xpath = '//p[@class="attrs"]/span[text()="description:"]/following-sibling::strong'
description = browser.find_by_xpath(xpath).first.text


回答2:

Would you be able to get the description using find_by_tag?

Find by Tag

browser.find_by_tag('span')

Then go iterate through all 'span' tags and look for the value of 'description'. I used the documentation here