首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python Selenium如何使用现有的chromedriver窗口?

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

Stack Overflow用户
提问于 2018-08-04 04:48:49
回答 2查看 446关注 0票数 1

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

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

如果这是不可能的,有没有替代的方法,你会推荐吗?

谢谢!

脚本:

代码语言:javascript
复制
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循环文件:

代码语言:javascript
复制
#!/bin/bash

while [ 1 ]
do
   python snkrs.py
done
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51679932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档