Discussion:
[python-win32] Pywin32 copying and pasting via mouse/keyboard
Kashif Ali
2014-12-09 19:41:19 UTC
Permalink
Hi,

I was helping someone can help me out. I am using the pywin32 module,
however I can't work out how to get the mouse to highlight specific text
on a webpage and save it into a variable.

I'm making an auto clicker, everything seems to be fine I can click on
browser url bar, enter a url etc... But if I wanted to select text I don't
know how to, then I would like for it save that into a variable, which I
can use to work out next steps.

for example, copy a number from the webpage put it into the clipboard then
transfer it into a variable to do some arithmetic on etc..

any help would be appreciated.
Tim Roberts
2014-12-09 21:23:36 UTC
Permalink
Post by Kashif Ali
I was helping someone can help me out. I am using the pywin32 module,
however I can't work out how to get the mouse to highlight specific
text on a webpage and save it into a variable.
I'm making an auto clicker, everything seems to be fine I can click on
browser url bar, enter a url etc... But if I wanted to select text I
don't know how to, then I would like for it save that into a variable,
which I can use to work out next steps.
for example, copy a number from the webpage put it into the clipboard
then transfer it into a variable to do some arithmetic on etc..
Your description is a little confusing, in part because it's not clear
what you mean by "auto clicker". Are you saying that you want to be
able to select text manually with your mouse, then do something that
uses the selected text? Traditionally, that has been done by a browser
plug-in, which means different modules for each different browser.

You can't do it with the normal UI automatic tools, because the browsers
don't use normal window controls. They implement their own controls,
then draw everything into a bitmap, then blit the bitmap to the screen.
Basically, the browsers have become an operating system unto themselves.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Tim Roberts
2014-12-10 00:02:07 UTC
Permalink
so when i say auto clicker, I mean using pywin32 and its mouse moving
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)|
|That's really delicate. If a field moves in the next browser version,
you're broken.
|
|However I want to be able to copy text in this automated fashion as
well. I know there is a clipboard function, however I don't know how
to copy text to it from the webpage, as i would need to select the
text and the send it to the clipboard, at which point i can then try
to get it into a variable.|
Yep, that's the problem. If it were a normal text box, you could send
EM_SETSEL to set the selection, them WM_COPY to copy to the clipboard.
But, as I said, the browser window is not a normal text box. It's
essentially a big bitmap. Now, continuing to apply the same logic you
have above, you could send LEFTDOWN at the start of the text, then MOVE,
then LEFTUP and the end of the text, then send a Ctrl-C event, but that
means you have to know exactly where the text is.

It might be easier to use COM to pull the HTML text of the page, and
parse that text for the information.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Tim Roberts
2014-12-10 00:26:59 UTC
Permalink
I could try to use that logic, left click move right then crtl+c it
might work.
I am interested in the COM to pull the HTML - however what happens if
its generated html (java script etc..)
Yep. There is no perfect solution. Some web pages intentionally make
it difficult to do what you are attempting. I always generate email
addresses with Javascript, just so that scummy spammer scrapers can't
easily grab the text.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Loading...