Python through JS control scroll bar Pull the full text through psutil get PID window handle, through Win32gui to make the program window front through Pyauto implement right-click menu and Save as Operation

Source: Internet
Author: User

1. Reference

Automate fast with Python + Selenium

Use Python + Selenium to implement a specified element of the page (a truncated graph element)

Use Python to get all the process PID and process name of the system

Reference method for Python to lock focus to the specified process window

2. Improve JS code, pull down and pull up, accurately determine whether the load is over
#!/usr/bin/env python
#-*-Coding:utf-8-*
Import time

From selenium import Webdriver
From Selenium.webdriver.support.ui import webdriverwait
From selenium.common.exceptions import nosuchelementexception, timeoutexception
From Selenium.webdriver.common.action_chains import Actionchains

defScroll_page (URL, browser='Chrome'): ifbrowser=='Chrome': Driver=Webdriver. Chrome ()Else: Driver= Webdriver. Firefox ()#Right-click Exception Actionchains (Driver). Context_click (E). Perform () #Driver = Webdriver. PHANTOMJS () #可截长图! But how long does it takes to load long Web pages? Driver.set_window_size (1200, 900) driver.get (URL)#Load PageStart =time.time ()Printdriver.title Driver.execute_script ("""(function () {var y = 0; var step = 1000; Window.Scroll (0, 0); function f () {if (Y < document.body.scrollHeight) {y + = step; Window.Scroll (0, y); SetTimeout (f, 100); 100 recursive Loop Call} else {if (Document.title.indexOf ("Scroll-done") < 0) { -1 not found, not yet executed driver.execute_script y-= step below; Window.Scroll (0, y); SetTimeout (f, 100); //window.scroll (0, 0); Document.title + = "Scroll-done"; }//else{//window.scroll (0, 0); }}} setTimeout (f, 1000); 1000}) (); """) #pull up to the last element "no more."Webdriverwait (Driver, $). Until (LambdaX:x.find_element_by_xpath ('//div[@style = "Text-align:center"]/em'))#or Trigger TimeoutException . #Stop the above JS to the end and pull the loop.Driver.execute_script ("""(function () {function f () {document.title + = "Scroll-done"; } setTimeout (f, 1000); })(); """) #<div class= "Js-infinite-layout" > #<div class= "Js-infinite-item" > #JetBrains Company officially released Pycharm 5RST = Driver.find_element_by_xpath ('//div[@class = "Js-infinite-item"][last ()]//div[@class = "header"]'). Text#cannot write text inside path () Printrstifrst! = U'JetBrains Company officially released Pycharm 5': RaiseRuntimeError ('wrong!!!') PrintTime.time ()-StartPrintDriver.title#Time.sleep (3) #执行js也需要时间Webdriverwait (Driver). Until (LambdaX:x.title = = U'Programmers | Coding Python Scroll-done')#or Trigger TimeoutException . PrintDriver.titlereturnDriver

3. Position the window via PID Process number and window caption and forward
Import Win32con
Import Win32gui
Import win32process
Import Psutil

defget_hwnds_for_pid (PID):defCallback (hwnd, HWNDs):ifWin32gui. IsWindowVisible (HWND) andWin32gui. IsWindowEnabled (HWND): _, Found_pid=win32process. GetWindowThreadProcessId (HWND)ifFound_pid = =Pid:hwnds.append (HWND)returnTrue HWNDs=[] Win32gui. EnumWindows (callback, HWNDs)returnHWNDsdefSet_process_foreground (Pid_part_name, Pid_window_text): Pids_target= [] forPidinchpsutil.pids (): P=Psutil. Process (PID)ifPid_part_nameinchP.name (). Lower ():#' Chrome.exe ' EXCEL. EXE 'pids_target.append (PID) forPidinchPids_target: forhwndinchget_hwnds_for_pid (PID):#92292 chrome.exe 137328 Programming Pie | Coding Python-google Chrome #EXCEL. EXE 857830 Microsoft excel-book1.xlsx #90644 firefox.exe 595556 Programming Pie | Coding Python Scroll-done-mozilla Firefox ifPid_window_text.encode ('GBK')inchWin32gui. GetWindowText (hwnd):PrintPID, Psutil. Process (PID). Name (), HWND, Win32gui. GetWindowText (HWND) Win32gui. SetForegroundWindow (HWND)return RaiseRuntimeError ('Process not found')
4. Right-click Operation via Autopy, and save page as
 fromAutopyImportKey, MousedefSave_result (Driver): Time_for_filename= Time.strftime ('%h%m%s') with open ('%s.html'%time_for_filename,'WB') as F:f.write (Driver.page_source.encode ('Utf-8'))            #e = Driver.find_element_by_xpath ('//img[@src = "/static/images/logo.png"]    #actionchains (Driver). Context_click (E). Perform ()    #Time.sleep (1.5)    #ESC exits the right-click menu    #Key.tap (key. K_escape)    #driver.set_window_position (0,0)    #window_position = driver.get_window_position () # {u ' x ': ten, U ' y ': ten}    #mouse.move (int (window_position[' x ']+150), int (window_position[' y ']+150))    #Right-click and left-click to avoid the wrong point to open the link    #Mouse.click (mouse. Right_button)    #Mouse.click (mouse. Left_button)        #The key is that the browser is located in foregroundSet_process_foreground (driver.name,driver.title) Key.tap ('s', key. Mod_control) Time.sleep (1.5) key.type_string (time_for_filename) time.sleep (0.5) Key.tap (key. K_return) Driver.save_screenshot ('%s.png'%time_for_filename)#driver.close ()if __name__=="__main__": Browser='Chrome'    #browser = ' Firefox 'URL ="http://codingpy.com"Driver=scroll_page (url,browser) Save_result (driver)Print ' All done'

Python through JS control scroll bar Pull the full text through psutil get PID window handle, through Win32gui to make the program window front through Pyauto implement right-click menu and Save as Operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.