I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'
Related
I'm trying to automate a specific task using python. I need to execute a specific task when a specific colour pops up on the display. For example:
During the automation process, when the code to open a web browser is executed and a web browser pops up,I need to make sure that it actually worked, by detecting the colour at the exact coordinate where the logo of the browser is situated. So that it confirms the browser is open, then I need to use that information to execute another code.
As we know, when we try to open a browser, there will be a lag to open up the browser. Since that lag will be different, I need to execute the next code only after opening the browser.
I have gone too far with my explanation, but if you got any idea from what I have just explained, please help me with what you know?
I'm trying to find a way to make my automation bot faster I realized that by searching only within the application window instead of the whole screen would give it a speed post how can i do it
You need to give us some code or what you already tried.
But you could use pywinauto package to use the application you want. For more details please check the How does it work.
You can get all applications open from Desktop function as:
from pywinauto import Desktop
windows = Desktop(backend="uia").windows()
window = [w.window_text() for w in windows]
So if you print(window) you will have for example:
['Zoom', 'python - how do i use Pyautogui locate on screen function within only one application window instead of the whole screen - Stack Overflow', 'Dictations - OneNote']
I need a browser in Selenium to work as if it's maximized, so that website that I process perceived it as maximized, but I want it really to be minimized, so that I could work in parallel while the script executes. The simple: driver.maximize_window() just maximizes windows.
So, is there a way for a window of browser to be maximized for website, but in reality minimized?
Several approaches here, depending on what your needs are.
First, most browser allow setting screen size when they are fired up. See details here How do I set browser width and height in Selenium WebDriver? Chrome, for example, can be controlled from outside of the selenium script either with ini files where you set your screen size properties or via command line arguments. See this page for specific args for chromium for example: https://peter.sh/experiments/chromium-command-line-switches/
Secondly, you could start your browser in headless mode and restrict it a specific size. If you do so, your code will run in the headless browser, meaning you can't see it on a real screen. See the miriad of stackoverflow question addressing this, including this one: How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
Thirdly, you could start a virtual display of a certain size and have your driver maximize the browser in all the tests. Tools like xvfb are a god send here. How do I run Selenium in Xvfb?
Lastly, you could use a real display that's set in the resolution/dpi you need. See answers like this Resize display resolution using python with cross platform support
You can try just running it in a small window instead of Maximized or Minimized.
Don't tell the browser to do anything in terms of size.
So where you tell Selenium
browser.maximize_window()
Erase or comment it out, and run the script that way.
It's either that or use multiple screens.
If you want to work while WebDriver is executed, you can created a virtual instance (with e.g. VirtualBox or HyperV) an run test suite in VM.
Maximized is just a browser size from the site perspective. Set the browser size to the screen resolution of your desktop and minimize the browser.
I need to write a python script which opens a website and when the website is completly opened it takes a screenshot of the opened website.
I wrote sth like this:
import webbrowser
import wx
wx.App()
link = "http://stackoverflow.com/questions"
webbrowser.get('firefox %s').open_new_tab(link)
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
It only opens a new tab in firefox but it doesnt take a screenshot of it :(
I want the solution to be cross-platform. Thanks for any help:)
EDIT:
The main problem here is that the script musnt take a picture BEFORE my webpage is completly opened. How to solve that issue?
I believe that you need different solutions for different operating systems. Use sys.platform to find out on which platform you are on. The rest you have to figure out yourself, but a quick internet search revealed:
On Linux systems, you can then take a screenshot as described here:
Take a screenshot via a python script. [Linux]
On Windows systems, you can base your solution on this answer:
Fastest way to take a screenshot with python on windows
On Mac systems, this will help:
Take screenshot in Python on Mac OS X
This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.
You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.
According to this question, you should be able to use ImageGrab to take a screenshot. Since ImageGrab uses PIL, this should be cross-platform. Here is some sample code to get you on your way
Hope this helps
I am trying to send a couple basic text commands to a flash program running in Firefox on Windows 7, but I am unable to get pywinauto working for me.
Right now, I have just been able to accomplish the very basic task of connecting to Firefox plugin-container by directing it to the path using the following code:
from pywinauto import application
app = application.Application()
app.connect_(path = r"c:\Program Files (x86)\Mozilla Firefox\plugin-container.exe")
The next step seems to be something to the effect of:
app.plugin-container.Edit.TypeKeys('Text')
However, I can't reference the plugin-container window using '.plugin-container', or any combination of those words. I have tried adding a title variable to the connect_() function and I have tried everything I can think of to find out how to type the command.
The example I am basing this off of is the notepad sample:
from pywinauto import application
app.start_(ur"notepad.exe")
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters!!!", with_spaces = True)
It doesn't matter to me if I use pywinauto or Firefox. If it is any easier to do this using a different module or Internet Explorer, I'm on board for whatever accomplishes the task. I am using Python version 2.7.2 and would prefer it over any version changes.
Any help at all is appreciated. I am pretty lost in all this.
As the author of pywinauto - I think you are going to have a hard time. pywinauto only really helps with standard windows controls, and I don't think that flash controls are implemented as standard windows controls (Buttons, Edit boxes, etc).
OFf the top of my head - I would think Sikuli may be a better starting point (http://sikuli.org/).
Another option may be 'http://code.google.com/p/flash-selenium/' - I just googled for "automating flash input" - and it turned up in one of the first articles I clicked.
Thanks for trying pywinauto - I just don't think it is best suited for Flash automation.