如何question
使用 Splinter 获取第一部分、下划线和最后一部分的文本并将其存储到变量中?
请参阅底部的 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 -------------
[]
[]
[]
我做错了什么,为什么错了,我应该如何更改我的代码?
browser.html
使用Splinter的功能检索了引用的 HTML(稍微编辑为“Jingle Bells”以更好地传达问题) :
<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>