简体   繁体   中英

how to use find_element after finding the hyperlink through click()

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."

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):]

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:

在此处输入图片说明

To return the text I like basketball and football. I like cooking. Thanks. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM