[英]I can't click the “buy now” button on amazon with Selenium Python
我正在尝试制作一个亚马逊机器人。 我在最后一个过程中,但我无法单击带有 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")
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")
您使用的是Full xpath
,因为您应该复制xpath
,它现在可以工作了。 而且我还添加了time.sleep
延迟,因为如果互联网有时滞后,它会有所帮助。 希望这可以解决问题。 是的,我使用了 Edge Driver,你可能想把它改回 chrome。 您应该像这样复制 xpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.