0

我一直试图让这段代码工作,但它不会。(该链接是一个占位符,我计划将其更改为 3060 Ti 以获得一个用于家庭装备。)它还说 goToCartBtn 被点击,但它没有。

import time
from selenium import webdriver
#For using chrome
browser = webdriver.Chrome('/Users/admin/Downloads/chromedriver')

#BestBuy RTX 3060 Ti webpage
browser.get('https://www.bestbuy.com/site/nvidia-geforce-rtx-nvlink-bridge-for-3090-cards-space-gray/6441554.p?skuId=6441554')


buyButton = False 

while not buyButton:
try:
    #If this works, then the button is not pytopen
    addToCartBtn = addButton = browser.find_element_by_class_name("btn-disabled")

    #Button isn't open, restart the script
    print("Button isn't ready yet.")

    #Refresh page after a delay
    time.sleep(1)
    browser.refresh()

except:

    addToCartBtn = addButton = browser.find_element_by_class_name("btn-primary")

    #Click the button 
    print("Button was clicked.")
    addToCartBtn.click()
    
    goToCartBtn = addButton = browser.find_element_by_class_name("btn-secondary")

    print("Go To Cart button was clicked.")
    goToCartBtn.click()

    checkoutBtn = addButton = browser.find_element_by_class_name("btn-lg")

    print("Checkout button clicked.")
    checkoutBtn.click()
    buyButton = True

错误信息如下所示:

Terminal Logs (if needed):
Button was clicked.
Go To Cart button was clicked.
Traceback (most recent call last):
  File "SniperBot.py", line 15, in <module>
    addToCartBtn = addButton = browser.find_element_by_class_name("btn-disabled")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn-disabled"}
  (Session info: chrome=88.0.4324.182)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "SniperBot.py", line 35, in <module>
    goToCartBtn.click()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=88.0.4324.182)
4

2 回答 2

1

您不需要“转到购物车”按钮,只需要一个 URL“bestbuy.com/cart”,我将它用于我的机器人。

于 2021-04-03T22:11:44.407 回答
0

您的 goToCartButton 选择器不够具体。

 goToCartButton = browser.find_element_by_class_name('btn-secondary')

使用这个选择器返回五个元素,Selenium 只会与它找到的第一个元素交互,根据 DOM,它不是“转到购物车”按钮。

相反,您可以使用 CSS 选择器来返回特定元素。使用 CSS:

goToCartButton = browser.find_element_by_css_selector('.go-to-cart-button .btn-secondary')

你现在应该可以点击按钮了。

于 2021-02-20T20:26:37.293 回答