简体   繁体   English

我无法使用 Selenium Python 在亚马逊上单击“立即购买”按钮

[英]I can't click the “buy now” button on amazon with Selenium Python

I am trying to make an amazon bot.我正在尝试制作一个亚马逊机器人。 I am on the last process but I can't click the buy now button with XPath or full XPath.I could find another unique selector because there is no id in that input.我在最后一个过程中,但我无法单击带有 XPath 或完整 XPath 的“立即购买”按钮。我可以找到另一个唯一选择器,因为该输入中没有 id。

在此处输入图像描述

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
import winsound


options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\QP\\AppData\\Local\\Google\\Chrome\\User Data")


driver = webdriver.Chrome(executable_path="C:\\webdrivers\\chromedriver.exe", chrome_options=options)
driver.get("https://www.amazon.com.tr/gp/product/9750748468?pf_rd_r=NCJRXGDB60BMBAWHZB0K&pf_rd_p=dbef927e-126e-4d59-a744-316eb26cc421&pd_rd_r=081be57a-efdb-4f6b-b282-72fb528b1bc5&pd_rd_w=Avc97&pd_rd_wg=XdzTG&ref_=pd_gw_unk")
try:
    addCart = driver.find_element_by_class_name("a-button-stack")
    addCart.submit()

    frequency = 2500
    duration = 50
    winsound.Beep(frequency, duration)

    completeTheShopping = driver.find_element_by_class_name("a-button-inner")
    completeTheShopping.click()

    buyNow = driver.find_element_by_xpath('/html//body/div[8]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[1]/span/span//span[1]/span/input')
    buyNow.click()


    print("Item found")

except NoSuchElementException:
    print("Item doesnt exist")

This will Solve it这将解决它

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
import winsound
import time

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\QP\\AppData\\Local\\Google\\Chrome\\User Data")

driver = webdriver.Chrome(executable_path="C:\\webdrivers\\chromedriver.exe", chrome_options=options)
driver.get("https://www.amazon.com.tr/gp/product/9750748468?pf_rd_r=NCJRXGDB60BMBAWHZB0K&pf_rd_p=dbef927e-126e-4d59-a744-316eb26cc421&pd_rd_r=081be57a-efdb-4f6b-b282-72fb528b1bc5&pd_rd_w=Avc97&pd_rd_wg=XdzTG&ref_=pd_gw_unk")
try:
    addCart = driver.find_element_by_class_name("a-button-stack")
    addCart.submit()

    frequency = 2500
    duration = 50
    winsound.Beep(frequency, duration)
    time.sleep(2)
    completeTheShopping = driver.find_element_by_class_name("a-button-inner")
    completeTheShopping.click()
    time.sleep(2)
    buyNow = driver.find_element_by_xpath('//*[@id="sc-buy-box-ptc-button"]/span/input')
    buyNow.click()


    print("Item found")

except NoSuchElementException:
    print("Item doesnt exist")

How I solved it我是如何解决的

You were using the Full xpath where as you should have copied the xpath and it works now.您使用的是Full xpath ,因为您应该复制xpath ,它现在可以工作了。 And I also added a time.sleep delay as it helps if the internet is laggy sometimes.而且我还添加了time.sleep延迟,因为如果互联网有时滞后,它会有所帮助。 Hope this Solves the Problem.希望这可以解决问题。 And yes I used Edge Driver you may wanna change it back to chrome.是的,我使用了 Edge Driver,你可能想把它改回 chrome。 You should copy the xpath like this您应该像这样复制 xpath 图片 Xpath 复制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM