bapwsta
bapwsta

Reputation: 15

Python Selenium how to use an existing chromedriver window?

I am making an automated python script which opens chromedriver on a loop until it finds a specific element on the webpage (using selenium) the driver gets. This obviously eats up recourses eventually as it is constantly opening and closing the driver while on the loop.

Is there a way to use an existing chromedriver window instead of just opening and closing on a loop until a conditional is satisfied?

If that is not possible is there an alternative way to go about this you would reccomend?

Thanks!

Script:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import pyautogui
import time
import os

def snkrs():
    driver = webdriver.Chrome('/Users/me/Desktop/Random/chromedriver')
    driver.get('https://www.nike.com/launch/?s=in-stock')
    time.sleep(3)
    pyautogui.click(184,451)
    pyautogui.click(184,451)
    current = driver.current_url
    driver.get(current)
    time.sleep(3.5)
    elem = driver.find_element_by_xpath("//* . 
    [@id='j_s17368440']/div[2]/aside/div[1]/h1")
    ihtml = elem.get_attribute('innerHTML')

    if ihtml == 'MOON RACER':
        os.system("clear")
        print("SNKR has not dropped")
        time.sleep(1)
    else:
        print("SNKR has dropped")
        pyautogui.click(1303,380)
        pyautogui.hotkey('command', 't')
        pyautogui.typewrite('python3 messages.py') # Notifies me by text
        pyautogui.press('return')
        pyautogui.click(928,248)
        pyautogui.hotkey('ctrl', 'z') # Kills the bash loop

snkrs()

Bash loop file:

#!/bin/bash

while [ 1 ]
do
   python snkrs.py
done

Upvotes: 1

Views: 1036

Answers (2)

JeffC
JeffC

Reputation: 25746

You are defining a method that contains the chromedriver launch and then running through the method once (not looping) so each method call generates a new browser instance. Instead of doing that, do something more like this...

url = 'https://www.nike.com/launch/?s=in-stock'
driver.get(url)

# toggle grid view
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Show Products as List']"))).click();

# wait for shoes to drop
while not driver.find_elements((By.XPATH, "//div[@class='figcaption-content']//h3[contains(.,'MOON RACER')]"))
    print("SNKR has not dropped")
    time.sleep(300) // 300s = 5 mins, don't spam their site
    driver.get(url)

print("SNKR has dropped")

I simplified your code, changed the locator, and added a loop. The script launches a browser (once), loads the site, clicks the grid view toggle button, and then looks for the desired shoe to be displayed in this list. If the shoes don't exist, it just sleeps for 5 mins, reloads the page, and tries again. There's no need to refresh the page every 1s. You're going to draw attention to yourself and the shoes aren't going to be refreshed on the site that often anyway.

Upvotes: 1

RKelley
RKelley

Reputation: 1119

If you're just trying to wait until something changes on the page then this should do the trick:

snkr_has_not_dropped = True

while snkr_has_not_dropped:

    elem = driver.find_element_by_xpath("//* .[ @ id = 'j_s17368440'] / div[2] / aside / div[1] / h1")
    ihtml = elem.get_attribute('innerHTML')

    if ihtml == 'MOON RACER':
        print("SNKR has not dropped")
        driver.refresh()
    else:
        print("SNKR has dropped")
        snkr_has_not_dropped = False

Just need to refresh the page and try again.

Upvotes: 0

Related Questions