【发布时间】:2019-01-11 18:41:01
【问题描述】:
我正在制作一个自动化的 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
【问题讨论】:
-
这里的意图是不断地重新加载网页,直到出现某些内容然后终止循环?
-
是的,这就是目的
标签: python selenium automation selenium-chromedriver