Reputation: 57
I used
continue_link=driver.find_elements_by_partial_link_text("contract")
to get a list of links. How do I apply find_elements on web elements I got already?
And how to return strings on web pages? Can find_element do that?
For example:
<div class="article-detail-text">
<h1>notice</h1>
<p class="article-date">release date:2019-05-22</p>
<p>hi:<br />
I like basketball and football.<br />
I like cooking.<br />
Thanks.</p>
And I want to return: "I like basketball and football. I like cooking. Thanks."
Upvotes: 1
Views: 150
Reputation: 168072
I believe the correct XPath expression to match the HTML snippet you provided would be something like:
//div[@class='article-detail-text']/descendant::p[contains(text(),'hi')]
Once you locate the relevant <div>
tag you will be able to get the text of the descendant <p>
tag using innerText property
print(driver
.find_element_by_xpath("//div[@class='article-detail-text']/descendant::p[contains(text(),'hi')]")
.get_attribute("innerText"))
Demo:
Upvotes: 1
Reputation: 33384
To return the text I like basketball and football. I like cooking. Thanks.
You can try the following option.
print(' '.join(driver.find_element_by_xpath("//p[contains(.,'hi:')]").text.split('hi:')[1].splitlines()))
This will print :
I like basketball and football. I like cooking. Thanks.
Upvotes: 0
Reputation: 50819
WebElement
has find_elements()
functions as well
continue_link = driver.find_elements_by_partial_link_text("contract")
for link in continue_link:
elements = link.find_elements_by_partial_link_text('')
And to get specific substring you can do
s1 = 'I like basketball and football'
s2 = 'like'
result = s1[s1.index(s2) + len(s2):]
Upvotes: 1