Python Splinter从表中获取价值(以下同级)

时间:2017-12-26 05:45:52

标签: python xpath splinter

鉴于此代码(" sleep"实例用于帮助显示正在进行的操作):

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)

我现在正试图从此代码运行后显示的表中获取值。我并不精通xpath命令,但基于对this question的响应,我尝试了这些,但无济于事:

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

我试过[2],因为我注意到" Name"之间有一个冒号(:)兄弟字符。和包含名称的单元格。我只想要名称本身的字符串值(以及表中的所有其他值)。

在这种情况下,我注意到一个不同的结构(在td内使用跨度而不仅仅是td)(我也试过td span [。=&#39; Name&#39;] ...但是没有骰子):< / p>

已更新以显示更多详细信息

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

1 个答案:

答案 0 :(得分:0)

这最终起作用了:

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