[英]What causes this error "'NavigableString' object has no attribute 'findAll'"?
I cant share the page but this is the sample html code that i wanna access我无法共享页面,但这是我想要访问的示例 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>
This is the code that im trying to run.这是我试图运行的代码。 What in trying to do is access the 2nd td in the first tr that i targeted.尝试做的是访问我定位的第一个 tr 中的第二个 td。 My expected result should be the Name text in side the 2nd 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)
The problem here is that item
is a NavigableString object which contains the value \\n
.这里的问题是item
是一个 NavigableString 对象,其中包含值\\n
。 A NavigableString is just a bit of text on the page whereas you are presumably expecting a list of td
items based on your code. NavigableString 只是页面上的一些文本,而您可能期望基于您的代码的td
项目列表。 To read more about NavigableStrings see here:要阅读有关 NavigableStrings 的更多信息,请参见此处:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring
I think the issue here is with the array notation ( [0]
) at the end of your findAll's.我认为这里的问题在于 findAll 末尾的数组符号 ( [0]
)。 You correctly get all of the tr
's, then take only one using the array notation, and then loop through the result of that which is a single tr object at this point.您正确地获得了所有的tr
,然后使用数组符号只取一个,然后循环遍历此时单个 tr 对象的结果。 Result of that is the td
tags and the newline spaces between.结果是td
标签和之间的换行符。
In short, get rid of the [0]
;)简而言之,去掉[0]
;)
End code i believe should be...我认为结束代码应该是......
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)
Note - also added .text
to this line in order to get the contents of the tag.注意 - 还向此行添加了.text
以获取标签的内容。
if td.text == i:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.