Reputation: 33
I cant share the page but this is the sample html code that i wanna access
<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. My expected result should be the Name text in side the 2nd td
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)
Upvotes: 0
Views: 366
Reputation: 300
The problem here is that item
is a NavigableString object which contains the value \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. To read more about NavigableStrings see here:
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. 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. Result of that is the td
tags and the newline spaces between.
In short, get rid of the [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.
if td.text == i:
Upvotes: 1