![](/img/trans.png)
[英]'NavigableString' object has no attribute 'strptime'
[英]What causes this error "'NavigableString' object has no attribute 'findAll'"?
我無法共享頁面,但這是我想要訪問的示例 html 代碼
<tr ng-repeat="record in records | orderBy: query.fullname" ng-click="create_dialog(record)" class="hand_cursor ng-scope" title="View Details" style=""> <td class="ng-binding">96685</td> <td class="ng-binding">Alvarado, Jacqueline C.</td> <td class="ng-binding">Nov 6, 2017</td> <td align="right" class="ng-binding">A1054</td> <td align="right" class="ng-binding">A1054 - A1054</td> <td align="right" class="ng-binding">87004000</td> </tr> <tr ng-repeat="record in records | orderBy: query.fullname" ng-click="create_dialog(record)" class="hand_cursor ng-scope" title="View Details"> <td class="ng-binding">33522</td> <td class="ng-binding">Bailey, Jacqueline B.</td> <td class="ng-binding">Jan 16, 1992</td> <td align="right" class="ng-binding">DL-46</td> <td align="right" class="ng-binding">DL-46 - DL-46</td> <td align="right" class="ng-binding">81313001</td> </tr>
這是我試圖運行的代碼。 嘗試做的是訪問我定位的第一個 tr 中的第二個 td。 我的預期結果應該是第二個 td 旁邊的 Name 文本
test_words = ['Jacqueline','abercrombie']
test_word = ['STEVEN']
# test_words = ['STEVEN','(',')','-','69807','21490','321']
for i in test_words:
search = self.driver.find_element_by_xpath('//*[@id="page-wrapper"]/div[3]/div/div/div/div/div[3]/tabletoolstrans/div/input')
search.clear()
search.send_keys(i)
search.send_keys(Keys.RETURN)
time.sleep(3)
soup = BeautifulSoup(self.driver.page_source,"html.parser")
for item in soup.findAll("tr", {"class": "hand_cursor ng-scope"})[1]:
for td in item.findAll("td")[1]:
if td == i:
print("Search :"+i+"")
buttons.save_csv(self, "Pass")
else:
print("Fail")
print("jaq")
time.sleep(3)
這里的問題是item
是一個 NavigableString 對象,其中包含值\\n
。 NavigableString 只是頁面上的一些文本,而您可能期望基於您的代碼的td
項目列表。 要閱讀有關 NavigableStrings 的更多信息,請參見此處:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring
我認為這里的問題在於 findAll 末尾的數組符號 ( [0]
)。 您正確地獲得了所有的tr
,然后使用數組符號只取一個,然后循環遍歷此時單個 tr 對象的結果。 結果是td
標簽和之間的換行符。
簡而言之,去掉[0]
;)
我認為結束代碼應該是......
test_words = ['Jacqueline','abercrombie']
test_word = ['STEVEN']
for i in test_words:
search = self.driver.find_element_by_xpath('//*[@id="page-wrapper"]/div[3]/div/div/div/div/div[3]/tabletoolstrans/div/input')
search.clear()
search.send_keys(i)
search.send_keys(Keys.RETURN)
time.sleep(3)
soup = BeautifulSoup(self.driver.page_source,"html.parser")
for item in soup.findAll("tr", {"class": "hand_cursor ng-scope"}):
for td in item.findAll("td"):
if td.text == i:
print("Search :"+i+"")
buttons.save_csv(self, "Pass")
else:
print("Fail")
print("jaq")
time.sleep(3)
注意 - 還向此行添加了.text
以獲取標簽的內容。
if td.text == i:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.