给定此代码(用于显示正在发生的事情的"睡眠"实例):
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命令中并不是很好,但是根据对这个问题的回答,我尝试了这些问题,但无济于事:
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],因为我确实注意到了一个结肠(:)"名称"和包含名称的单元格之间的兄弟姐妹字符。我只希望名称本身的字符串值(以及表中的所有其他值)。
在这种情况下更新以显示更多详细信息
<tr>
<td>
<span class="MOECSBold">Name</span>
</td>
<td>:</td>
<td>
<span id="ContentPlaceHolder1_lblName" class="MOECSNormal">MICHAEL WILLIAM LANCE </span>
</td>
</tr>
这最终工作:
browser.find_by_xpath("//td[span='Name']/following-sibling::td")[1].value