启动chromedriver后,Python脚本停止在CMD中执行

时间:2020-07-09 14:58:51

标签: python selenium selenium-chromedriver

该代码在IDLE中工作正常,但是双击文件或从命令行运行时,该程序的其余部分似乎无法正常工作,因为我的按键处理程序对按键不起作用。我相信这可能与chromedriver接管cmd窗口有关,但是我对python还是很陌生,我不确定。附带的是代码,它将打开chromedriver窗口,并且不绑定在IDLE中绑定的按键:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import Select
import keyboard
from tkinter import *
from tkinter import messagebox

address = {}

chrome_options = webdriver.ChromeOptions(); 
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
chrome_options.add_argument("--disable-extensions")
chromeDriverPath = 'C:/ChromeDriver/chromedriver.exe'

def keyboardHandler(keyboardEvent):
    if keyboardEvent.name == "f7":
        copyAddress(driver)
    elif keyboardEvent.name == "f9":
        paste(driver)

#Script will launch browser when f7 is pressed
keyboard.wait('f7')
driver = webdriver.Chrome(chromeDriverPath, options=chrome_options)
driver.get('http://www.ebay.co.uk/')

keyboard.on_press(keyboardHandler)

1 个答案:

答案 0 :(得分:0)

headless/monitor

我并不是无所不用其极,但是在基于Linux的系统上我必须这样做。在IDLE中,将隐式设置显示内容

os.environ["DISPLAY"] = ":0.0"