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

Python Selenium如何使用现有的chromedriver窗口?,python,selenium,automation,selenium-chromedriver,Python,Selenium,Automation,Selenium Chromedriver,我正在制作一个自动python脚本,在循环中打开chromedriver,直到它在驱动程序获得的网页上找到一个特定元素(使用selenium)。这显然最终会消耗资源,因为它在循环中不断地打开和关闭驱动程序 有没有一种方法可以使用现有的chromedriver窗口,而不是在满足条件之前打开和关闭循环 如果这是不可能的,有没有其他方法可以解决这个问题 谢谢 脚本: from selenium.webdriver.common.keys import Keys from selenium import

我正在制作一个自动python脚本,在循环中打开chromedriver,直到它在驱动程序获得的网页上找到一个特定元素(使用selenium)。这显然最终会消耗资源,因为它在循环中不断地打开和关闭驱动程序

有没有一种方法可以使用现有的chromedriver窗口,而不是在满足条件之前打开和关闭循环

如果这是不可能的,有没有其他方法可以解决这个问题

谢谢

脚本:

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()
#!/bin/bash

while [ 1 ]
do
   python snkrs.py
done
Bash循环文件:

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()
#!/bin/bash

while [ 1 ]
do
   python snkrs.py
done

如果您只是想等待页面上的某些内容发生更改,那么这应该可以做到:

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

只需刷新页面并重试。

您正在定义一个包含chromedriver启动的方法,然后在该方法中运行一次(而不是循环),因此每个方法调用都会生成一个新的浏览器实例。与其这样做,不如做些更像这样的事情

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")

我简化了您的代码,更改了定位器,并添加了一个循环。该脚本启动浏览器(一次),加载站点,单击“栅格视图切换”按钮,然后在此列表中查找所需的鞋。如果鞋子不存在,它只会休眠5分钟,重新加载页面,然后重试。没有必要每1s刷新一次页面。你会引起人们对你自己的注意,而且鞋子也不会经常在网站上刷新。

这里的意图是不断地重新加载网页,直到出现一些东西,然后终止循环吗?是的,这就是意图谢谢你的回复,我目前收到以下错误。我可能明天就能解决,但如果你能轻松解决的话,lmk!错误:selenium.common.exceptions.WebDriverException:消息:无效参数:“using”必须是string@bapwsta在哪一行?第16行(我在脚本中添加了一些行):“而不是driver.find_元素((By.XPATH,//div[@class='figcaption-content']//h3[contains(,'MOON RACER')]”):我成功了,再次感谢您的回复,我期待着在将来使用我从您的版本中获得的知识。这不再是一个黑客工作了,哈哈,接受并投票,我的投票还没有公开显示。