2

What I'm trying to do is making nike product auto buyer the problem is after selecting size it doesn't let me click through selenium I even tried to click manually but nothing pops up this is my code where I try to click (not full code):

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)

I use window.scroll to go to that element because if I don't it throws error saying element is not interactable and yes checkout is being only clickable from real chrome. Thanks

SadLandscape
  • 75
  • 1
  • 8
  • can you share the exact page or describe a flow of your code facing with that problem? – Prophet Jul 15 '21 at 17:37
  • ah yes here's the link: https://www.nike.com/t/air-zoom-pegasus-38-womens-running-shoe-wide-gg8GBK/CW7358-500 the code I provided was the code that is must've clicked Add to Bag button it clicks the size but when it comes to clicking Add to Bag it doesn't throw error it highlights button like when you mouse hover button. Thanks – SadLandscape Jul 15 '21 at 17:44
  • I saw the web page, added the product to cart, saw your code. Really do not understand your code. Please provide ALL your code. – Prophet Jul 15 '21 at 17:49
  • 1
    ok I know it's kind of messed up but I edited *editing – SadLandscape Jul 15 '21 at 17:50
  • not sure if this is *the* issue, but chrome.execute_script("arguments[0].scrollIntoView(true);") should be chrome.execute_script("arguments[0].scrollIntoView(true);", e) – Jeremy Kahan Jul 16 '21 at 07:54
  • @Jeremy Didn't work I think they have some kind of bot protection because I was able to use my real browser in selenium and they still didn't let me click. One thing that I knew from it was when selenium ran and I canceled it it said chrome is being controlled by automation software I manually typed url went selected size and it worked any thoughts? – SadLandscape Jul 16 '21 at 08:04
  • So the line wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="gen-nav-footer"]/nav/button'))).click() gets stuck for me. What is it supposed to be ciicking? – Jeremy Kahan Jul 16 '21 at 08:40
  • the add to bag runs a script, which chrome does not seem to want to do. Try options.add_argument("--allow-running-insecure-content") options.add_argument("--disable-web-security"); options.add_argument("--allow-insecure-localhost"); – Jeremy Kahan Jul 16 '21 at 09:29
  • @Jeremy-Kahan It was supposed to be clicking a pop up that asks you if you want to set your location in US and the 2rd one didn't work – SadLandscape Jul 16 '21 at 09:55

0 Answers0