2017-09-20 101 views
0

我已經創建了一個python腳本,用於在瀏覽器上執行一些操作並截取它。 當系統沒有鎖定我的腳本工作正常。 但是當系統被鎖定時,它會截取我鎖定的屏幕而不是瀏覽器。 如何在系統鎖定時拍攝瀏覽器截圖?當系統處於鎖定狀態時以python截圖

我使用:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 
from selenium import webdriver 
import time 
from selenium.webdriver.chrome.options import Options 
from PIL import ImageGrab 
import win32com.client as win32 
import re 

options = webdriver.ChromeOptions() 
options.add_argument("--start-maximized") 

driver = webdriver.Chrome(chrome_options=options) 

chromedriver = 'C:\Python27\chromedriver_win32\chromedriver.exe' 
driver = webdriver.Chrome(chromedriver) 
link=driver.get('http://10.113.174.197/testlink/login.php? 
note=logout&viewer=') 

username = "xyz" 
password = "xyz" 

def login(): 
    inputid = driver.find_element_by_id('tl_login') 
    inputid.send_keys(username) 
    inputpass = driver.find_element_by_name('tl_password') 
    inputpass.send_keys(password) 
    signin = driver.find_element_by_xpath('//*[@id="login"]/div[3]/input') 
    signin.click() 
    time.sleep(3) 

def testreport(): 

    driver.get("http://10.113.174.197/testlink/lib/results/resultsByTesterPerBuild.php?format=0&tplan_id=537") 
#driver.get('http://10.113.174.194/testlink/lib/results/resultsGeneral.php?format=0&tplan_id=125194') 

def screenshot(): 
    img = ImageGrab.grab() 
    img.save('C:\Users\XYZ\Downloads\screenshot.png') 
    img.show() 

def mail(): 
    outlook = win32.Dispatch('outlook.application') 
    mail = outlook.CreateItem(0) 
    mail.To = '[email protected]' 
    mail.Subject = 'xyz ' 
    attachment =mail.Attachments.Add("C:\Users\XYZ\Downloads\screenshot.png") 
    mail.Attachments.Add('C:\Users\MA299445\Downloads\screenshot.png') 
    mail.Send() 




if __name__ == '__main__': 
    login() 
    testreport() 
    time.sleep(3) 
    screenshot() 
    time.sleep(3) 
    mail() 

環境: 的Python 2.3 的Windows 10

+0

給你的完整代碼或這是它? –

+0

@manan_kalariya:我已經添加了我的完整代碼,請檢查。 –

回答

0

您可以用Selenium來捕獲的屏幕截圖。即使屏幕鎖定,它也可以工作。

from selenium import webdriver 
import os 
driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe") 
#removes the file if it already exists 
try: 
    os.remove('E:\dhd.png') 
except WindowsError: 
    #print("no file found") 
    pass 
# Maximize the browser window 
driver.maximize_window() 
driver.get("https://accounts.google.com/ServiceLogin/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=AddSession") 
driver.save_screenshot('e:\dhd.png') 
driver.quit() 
相關問題