JZ115
JZ115

Reputation: 1

XPath delivering blank text

I am trying to pull the text out of a tag that follows an element I'm starting with. The HTML looks like this, with multiple entries of the same structure:

<h5>
    <a href="link">Title</a>
</h5>
<div class="author">
    <p>"Author A, Author B"</p>
</div>
<div id="abstract-more#####" class="collapse">
  <p>
    <strong>Abstract:</strong>
    "Text here..."
  </p>
  <p>...</p>

So once I've isolated a given title element/node (stored as 'paper'), I want to store the author and abstract text. It works when I use this to get the author:

author = paper.find_element_by_xpath("./following::div[contains(@class, 'author')]/p").text

But is returning a blank output for 'abstract' when I use this:

abstract = paper.find_element_by_xpath("./following::div[contains(@id, 'abstract-more')]/p").text

Why does it work fine for the author but not for the abstract? I've tried using .// instead of ./ and other slight tweaks but to no avail. I also don't know why it's not giving an error out and saying it can't find the abstract element and is instead just returning a blank...

Upvotes: 0

Views: 315

Answers (3)

Andrei
Andrei

Reputation: 5637

Try this:

//div[contains(@id, 'abstract-more')]/p[1]

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

You can try this xpath :

//div[@class="author"]/following-sibling::div[contains(@id,'abstract-more')]/p[1] 

in code :

author = paper.find_element_by_xpath("//div[@class="author"]/following-sibling::div[contains(@id,'abstract-more'')]/p[1]")  
print(author.text)

Upvotes: 0

Subburaj
Subburaj

Reputation: 2334

Please use starts-with in xpath instead of contains.

XPath: .//div[starts-with(@id, 'abstract-more')]/p"

abstract = paper.find_element_by_xpath(".//div[starts-with(@id, 'abstract-more')]/p").text

Upvotes: 0

Related Questions