Python Splinter Get Value From Table (Following-Si

2019-06-13 20:13发布

问题:

Given this code ("sleep" instances used to help display what's going on):

from splinter import Browser
import time

with Browser() as browser:
    # Visit URL
    url = "https://mdoe.state.mi.us/moecs/PublicCredentialSearch.aspx"
    browser.visit(url)
    browser.fill('ctl00$ContentPlaceHolder1$txtCredentialNumber', 'IF0000000262422')

# Find and click the 'search' button
button = browser.find_by_name('ctl00$ContentPlaceHolder1$btnSearch')
# Interact with elements
button.first.click()
time.sleep(5)

#Only click the link next to "Professional Teaching Certificate Renewal"
certificate_link = browser.find_by_xpath("//td[. = 'Professional Teaching Certificate Renewal']/following-sibling::td/a") 
certificate_link.first.click()
time.sleep(10)

I am now trying to get the values from the table that shows after this code runs. I am not well-versed in xpath commands, but based on the response to this question, I have tried these, to no avail:

name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/a")
name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/[1]")
name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/[2]")

I tried [2] because I do notice a colon (:) sibling character between "Name" and the cell containing the name. I just want the string value of the name itself (and all other values in the table).

I do notice a different structure (span is used within td instead of just td) in this case (I also tried td span[. ='Name']... but no dice):

Updated to show more detail

<tr>
 <td>
  <span class="MOECSBold">Name</span>
 </td>
 <td>:</td>
 <td>
     <span id="ContentPlaceHolder1_lblName" class="MOECSNormal">MICHAEL WILLIAM LANCE  </span>
 </td>
</tr>

回答1:

This ended up working:

browser.find_by_xpath("//td[span='Name']/following-sibling::td")[1].value