2

questionSplinter を使用して、最初、下線、および最後の部分のテキストを取得し、変数に格納するにはどうすればよいですか?

下部の HTML を参照してください。次の変数に次の値を持たせたい:

first_part = "Jingle bells, jingle bells, jingle all the"
second_part = "_______"
third_part = "! Oh what fun it is to ride in one-horse open sleigh!"

私はここに行き、XPATHを使用しました

//*[@id="question_container"]/div[1]/span/text()[1] #this is first_part
//*[@id="question_container"]/div[1]/span/span      #this is second_part
//*[@id="question_container"]/div[1]/span/text()[2] #this is third_part

それらを以下のHTMLに適用しました。彼らはテストで必要な値を返しましたが、私のプログラムでは、Splinter はそれらを拒否しているようです:

first_part = browser.find_by_xpath(xpath = '//*[@id="question_container"]/div[1]/span/text()[1]').text
second_part = browser.find_by_xpath(xpath = '//*[@id="question_container"]/div[1]/span/span').text
third_part = browser.find_by_xpath(xpath = '//*[@id="question_container"]/div[1]/span/text()[2]').text

print first_part
print second_part
print third_part

--------------    OUTPUT     -------------

[]
[]
[]

何が間違っているのか、なぜ間違っているのか、コードをどのように変更すればよいのでしょうか?

参照されている HTML (問題をよりよく伝えるために「ジングルベル」に少し編集されています) はbrowser.html、Splinter の機能を使用して取得されました。

<div id="question_container" style="display: block;">
<div class="question_wrap">

<span class="question">Jingle bells, jingle bells, jingle all the
<span class="underline" style="display: none;">_______</span>
<input type="text" name="vocab_answer" class="answer" id="vocab_answer"></input>
! Oh what fun it is to ride in one-horse open sleigh!</span>

</div></div>
4

1 に答える 1