简体   繁体   English

Python Selenium如何使用现有的chromedriver窗口?

[英]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. 我正在制作一个自动的python脚本,该脚本会在循环中打开chromedriver,直到它在驱动程序获取的网页上找到特定元素(使用硒)为止。 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? 有没有一种方法可以使用现有的chromedriver窗口,而不仅仅是在循环中打开和关闭直到满足条件?

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: Bash循环文件:

#!/bin/bash

while [ 1 ]
do
   python snkrs.py
done

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. 只需刷新页面,然后重试。

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. 您正在定义一个包含chromedriver启动的方法,然后在该方法中运行一次(不循环),因此每个方法调用都会生成一个新的浏览器实例。 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. 如果鞋子不存在,它会休眠5分钟,重新加载页面,然后重试。 There's no need to refresh the page every 1s. 无需每1秒刷新一次页面。 You're going to draw attention to yourself and the shoes aren't going to be refreshed on the site that often anyway. 您将引起自己的注意,并且无论如何网站上都不会更新鞋子。

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

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