Detect disabled button for checkout bot

Viewed 35

Sup everyone.

I'm building a checkout bot for myself and part of it is a pandas dataframe, which prints the item title + if the item is available or not. I can print the item title easily, but the only difference between an item in and out of stock is that the 'add to cart' button for items out of stock are disabled (all items are on the same page)

Here's the code for the out of stock item button:

<div class="StyledButtonWrapper-sc-1h3ts0h-0 gOOwkr"><button type="button" disabled="" class="StyledButton-sc-140xkaw-1 iAEEJh StyledButton-sc-1bveayl-0 gvJvME" data-test="a2c-Button"><div width="24" height="24" class="Icon-sc-1vrq823-0 iMdSTb"><svg width="32" height="32" viewBox="0 0 24 24"><path d="M20 20a2 2 0 1 1-2-2 2 2 0 0 1 2 2zM8 18a2 2 0 1 0 2 2 2 2 0 0 0-2-2zM21.5 4H5.72l-.56-1a2 2 0 0 0-1.74-1H2.5a.5.5 0 0 0-.5.5v1a.5.5 0 0 0 .5.5h.92l4.28 7.49a2.19 2.19 0 0 0 .27.35l-1.86 3.7a.77.77 0 0 0 0 .71l.22.38A.76.76 0 0 0 7 17h12.5a.5.5 0 0 0 .5-.5v-1a.5.5 0 0 0-.5-.5H8.62l1-2.09a2.74 2.74 0 0 0 .64.09h6.23A2.94 2.94 0 0 0 19 11.49l2.35-4.13A4.76 4.76 0 0 0 22 5v-.5a.5.5 0 0 0-.5-.5z"></path></svg></div></button></div>

Here's also a screenshot of the code if that helps: disabled add to cart button

In comparison this a working add-to-cart-button:

<div class="StyledButtonWrapper-sc-1h3ts0h-0 gOOwkr"><button type="button" class="StyledButton-sc-140xkaw-1 yIKjw StyledButton-sc-1bveayl-0 gvJvME" data-test="a2c-Button"><div width="24" height="24" class="Icon-sc-1vrq823-0 iMdSTb"><svg width="32" height="32" viewBox="0 0 24 24"><path d="M20 20a2 2 0 1 1-2-2 2 2 0 0 1 2 2zM8 18a2 2 0 1 0 2 2 2 2 0 0 0-2-2zM21.5 4H5.72l-.56-1a2 2 0 0 0-1.74-1H2.5a.5.5 0 0 0-.5.5v1a.5.5 0 0 0 .5.5h.92l4.28 7.49a2.19 2.19 0 0 0 .27.35l-1.86 3.7a.77.77 0 0 0 0 .71l.22.38A.76.76 0 0 0 7 17h12.5a.5.5 0 0 0 .5-.5v-1a.5.5 0 0 0-.5-.5H8.62l1-2.09a2.74 2.74 0 0 0 .64.09h6.23A2.94 2.94 0 0 0 19 11.49l2.35-4.13A4.76 4.76 0 0 0 22 5v-.5a.5.5 0 0 0-.5-.5z"></path></svg></div></button></div>

This is my code to print all item titles on the page and it works (don't mind the indentations):

    items = soup.find('div', {'class': 'StyledRow-x4c83j-0 koBzMt'})

 print(len(items.findAll('div', {'class': 'StyledCardWrapper-sc-1j5kbg6-0 cOnriV'})))
 for item in items.findAll('div', {'class': 'StyledCardWrapper-sc-1j5kbg6-0 cOnriV'}):
     item_title = item.find('p', {'class': 'BaseTypo-sc-1jga2g7-0 izkVco StyledInfoTypo-sc-1jga2g7-1 doYUxh'})
     print(item_title.text)
1 Answers
## this is how you find elements with 'disabled' tag
def is_disabled(tag):
    return tag.has_attr('disabled')

## this is the current bs4 syntax
items = soup.select('div.StyledRow-x4c83j-0.koBzMt')

print(len(items.select('div.StyledCardWrapper-sc-1j5kbg6-0.cOnriV')))
for item in items.select('div.StyledCardWrapper-sc-1j5kbg6-0.cOnriV'):
    item_title = item.select_one('p.BaseTypo-sc-1jga2g7-0.izkVco.StyledInfoTypo-sc-1jga2g7-1.doYUxh')
     print(item_title.text)
        
## you can adapt this to look only inside parent element, etc
disabled_buttons = soup.find_all(is_disabled)

I could not test this, so your mileage might vary.

Related