Python Splinter从表中获取价值(后续同级) [英] Python Splinter Get Value From Table (Following-Sibling)
问题描述
给出以下代码("sleep"实例用于帮助显示正在发生的事情):
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)
我现在正在尝试从运行此代码后显示的表中获取值.我不熟悉xpath命令,但是基于对
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]")
我尝试了[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).
在这种情况下(我也尝试过td span [.='Name'] ...但没有骰子),我确实注意到了不同的结构(在td中使用了span而不是td):
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):
已更新以显示更多详细信息
<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
这篇关于Python Splinter从表中获取价值(后续同级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!