1

我想要做的是让耐克产品自动购买者问题是在选择尺寸后它不允许我点击硒我什至尝试手动点击但没有弹出这是我尝试点击的代码(不完整代码):

from selenium import webdriver
from selenium.common.exceptions import JavascriptException
from selenium.webdriver import ChromeOptions
import re
from bs4 import BeautifulSoup
import requests
import json
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os

user = os.environ['USERNAME']
snkrsurl = "https://www.nike.com/t/air-zoom-pegasus-38-womens-running-shoe-wide-gg8GBK/CW7358-500" #input("Please input your SNKRS url \n")
size = float(input("Please input size \n"))
options = ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-logging'])
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("detach",True)
options.add_argument("--disable-notifications")

chrome = webdriver.Chrome(options=options)
if "https://" in snkrsurl:
    pass
elif "http://" in snkrsurl:
    pass
else:
    snkrsurl = "http://"+snkrsurl

chrome.get(snkrsurl)
with requests.Session() as session:
    soup = BeautifulSoup(session.get(snkrsurl).text, features="lxml")
script = soup.find("script", string=re.compile('INITIAL_REDUX_STATE')).string
redux = json.loads(script[script.find('{'):-1])
products = redux["Threads"]["products"]
wait = WebDriverWait(chrome, 15)
def step1(i,v):
    for key, product in products.items():
        if float(product["skus"][i]["nikeSize"]) == v:
            print("Found")
            if v.is_integer():
                wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="gen-nav-footer"]/nav/button'))).click()
                wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='{}']".format(int(v))))).click()
                chrome.execute_script("window.scroll(0,609)")
                wait.until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Add to Bag"]'))).click()
                break
            else:
                wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="gen-nav-footer"]/nav/button'))).click()
                wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='{}']".format(v)))).click()
                e = chrome.find_element_by_css_selector("#floating-atc-wrapper > div > button.ncss-btn-primary-dark.btn-lg.add-to-cart-btn")
                chrome.execute_script("arguments[0].scrollIntoView(true);")
                e.click()
                break
        else:
            pass
for i,v in products.items():
    global length 
    length = len(v['skus'])
    break
for i in range(length):
    length -=1
    step1(length,size)

我使用 window.scroll 转到该元素,因为如果我不这样做,它会抛出错误,说元素不可交互,是的,结帐只能从真正的 chrome 中单击。谢谢

4

0 回答 0