簡體   English   中英

Python Splinter從表中獲取價值(后續同級)

[英]Python Splinter Get Value From Table (Following-Sibling)

給定以下代碼(“ 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命令,但是基於對這個問題的回答 ,我嘗試了這些,但無濟於事:

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],因為我確實注意到“名稱”和包含名稱的單元格之間的冒號(:)兄弟字符。 我只想要名稱本身的字符串值(以及表中的所有其他值)。

我確實注意到在這種情況下(我也嘗試過td span [。='Name'] ...但沒有骰子)使用不同的結構(在td中使用了span而不僅僅是td):

更新以顯示更多詳細信息

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM