0

I'm trying to create a 'bot' to buy a graphics card. I've downloaded a pre-made script, and i'm trying to adjust to my needs.

Script take me to the site in Firefox, finds the button I am trying to look for using the following code:

findAllCards = soup.find('button', {'class': 'Button__StyledButton-iESSlv dJJJCD Button-dtUzzq kHUYTy'})

This works. However, when I an trying to click the button, I am unable to as I have no idea what I am suppose to find here: driverWait(driver, 'css', '.space-b center')

Webpage I'm using to test is: https://www.currys.co.uk/gbuk/gaming/console-gaming/controllers/xbox-wireless-controller-carbon-black-10211565-pdt.html

Full code here:

driver.get(url)
    while True:
        html = driver.page_source
        soup = bs4.BeautifulSoup(html, 'html.parser')
        wait = WebDriverWait(driver, 15)
        wait2 = WebDriverWait(driver, 2)
        try:
            findAllCards = soup.find('button', {'class': 'Button__StyledButton-iESSlv dJJJCD Button-dtUzzq kHUYTy'})
            if findAllCards:
                print(f'Button Found!: {findAllCards.get_text()}')

                # Clicking Add to Cart.
                time.sleep(.3)
                print('Click')
                driverWait(driver, 'css', '.space-b center')
                print('Click1')
                time.sleep(2)

Thanks :)

2 Answers 2

1

Your findAllCards above returns 3 web elements, not 1. Assuming you are trying to click on the Add to Basket button:

findAllCards = driver.find_element_by_xpath("//div[@id='product-actions']//div[@data-component='add-to-basket-button-wrapper']//button")

findAllCards.click()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for you quick response. I've tried the above and this doesn't seem to work.
woops, my mistake. had the incorrect ID. Try again
try: findAllCards = soup.find('button', {'class': 'Button__StyledButton-iESSlv dJJJCD Button-dtUzzq kHUYTy'}) if findAllCards: print(f'Button Found!: {findAllCards.get_text()}') # Clicking Add to Cart. time.sleep(.3) print('Click') findAllCards = driver.find_element_by_xpath("//div[@id='product-main']//div[@data-component='add-to-basket-button-wrapper']//button") findAllCards.click() print('Click1') time.sleep(2)
Get rid of the soup.find piece and all the rest of it. Just try with the code I provided above after you reach that URL. See if it works
0

Some odd thing with element to be interactable.

wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
try:
    elem=wait.until(EC.presence_of_element_located((By.XPATH,"//button[contains(.,'Add to basket')]")))
    driver.execute_script("arguments[0].click();", elem)
except Exception as e:
    print(str(e))

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

1 Comment

Thanks, will give this a try tomorrow and report back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.