我正在制作一个自动化的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()
Bash循环文件:
#!/bin/bash
while [ 1 ]
do
python snkrs.py
done
https://stackoverflow.com/questions/51679932
复制相似问题