Selenium将HTML发送到stdout并将错误发送到stderr

时间:2016-02-16 00:16:33

标签: python selenium

我有这个脚本工作..但我想将HTML发送到stdout并将错误(例外)发送到stderr

import time
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

display = Display(visible=0, size=(800, 600))
display.start()

def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver

def lookup(driver, query):
    driver.get("http://www.google.com")

    try:
        box = driver.wait.until(EC.presence_of_element_located(
            (By.NAME, "q")))
        box.send_keys(query)
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "btnG")))
        button.click()
    except TimeoutException:
        print "Box or Button not found in google.com"

if __name__ == "__main__":
    driver = init_driver()
    lookup(driver, "Selenium")
    time.sleep(5)
    driver.quit()
    display.stop()

1 个答案:

答案 0 :(得分:0)

我想

except TimeoutException:
    print>>sys.stderr, "Box or Button not found in google.com"

但默认情况下sys.stdout,sys.stderr都打印到我认为的控制台