[python-win32] equivalent selenium findElement(By.xpath...) with win32com
laurent solano
laurent.solano at gmail.com
Wed Mar 9 12:34:48 EST 2016
Thanks for your proposal.
I had already identified and look at the documentation about splinter.
Sound great, but there is one missing feature : i need to automate an
already open browser, where i'm already log in.
With win32com, i can do it. below my code:
class WindowMgr:
"""
Source :
http://stackoverflow.com/questions/2090464/python-window-activation
Classe de gestion des fenêtres, notamment pour faire des recherche avec
expressions régulières
"""
def __init__ (self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name = None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
'''Pass to win32gui.EnumWindows() to check all the opened
windows'''
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
class ie_mgr():
"""
Classe de gestion de la fenêtre ie. Permet d'obtenir un objet
représentant la page ie de suviefluid
"""
def __init__ (self,titre_fenetre):
"""Constructor"""
# Source : http://win32com.goermezer.de/content/view/168/131/
_clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' # clé du registre
correspondant au ShellWindows
_ShellWindows = win32com.client.Dispatch(_clsid)
for i in range(_ShellWindows.Count): # Recherche de la
fenêtre IE à manipuler
if re.match(r"suivefluid
production*",_ShellWindows(i).LocationName):
print("valeur ok : ", i)
_index_shell = i
break
try:
self.ie = _ShellWindows(_index_shell)
self.doc = self.ie.Document
self.bdy = self.doc.body
self.Frame_haut = self.bdy.children(0)
self.Frame_bas = self.doc.body.children(1) # On focus sur la
frame contenant les valeurs à insérer (la frame "bas")
except NameError as e:
print(e)
SystemExit
if __name__ == "__main__":
fenetre_a_traiter = r".*windows_name.*"
sf = ie_mgr(fenetre_a_traiter) #Instanciation du webbrowser
w = WindowMgr() #Instanciation de la
fenêtre à traiter
w.find_window_wildcard(fenetre_a_traiter)
=> After this, i can manipulate my browser like this :
dURL = "my_URL" # URL
sf.ie.Navigate(dURL,0,"frame_name") #Go écran missions
(...)
w.set_foreground() #Affichage de sf en premier plan
=> After this, i can seend keys to manipulate my browser
_shell = win32com.client.Dispatch("WScript.Shell") # On instancie un objet
permettant de simuler les touches clavier
_shell.SendKeys("{TAB 34}") #Tab jusqu'à l'onglet mission
_shell.SendKeys("{ENTER}")
_shell.SendKeys("612") #Imput numéro de mission à rechercher
_shell.SendKeys("{ENTER}") #Clic bouton rechercher
and so on.
I need to replace the sendkeys part by some good code like
#Supposing "sf" is a splinter or selenium representation of my already
running browser :
sf.find_by_xpath(('//table[@id='tablename']/tbody/tr[1]/td[2]').click() (or
something similar)
nota : you hava my code Tim ;-).
Laurent
2016-03-08 1:19 GMT+01:00 Christopher Nilsson <chris at slort.org>:
> Not really via the win32 api, but if you're looking for the python
> equivalent of this selenium use-case, you should check out Splinter (
> http://splinter.readthedocs.org/en/latest/).
>
> You'll need phantomjs, or any other compatible web "driver" installed as
> well though for this to work.
>
> I imagine achieving similar functionality with just win32 api will be a
> pretty massive effort...
>
> On Mon, 7 Mar 2016 at 22:15 laurent solano <laurent.solano at gmail.com>
> wrote:
>
>> Hi,
>>
>> For now, i make automation by simulating the keyboard. It's working, but not satisfying.
>>
>> What I want to do is the equivalent in Python win32 of this line of code (from java selenium) :
>>
>> - webDriver.findElement(By.name("valider")).click();
>>
>> - confirmation = webDriver.findElement(By.xpath("//table[@id='tablename']/tbody/tr[1]/td[2]")).getText();
>>
>>
>> Using doc.valider.click() and doc.TheForm.valider.click() is not working.
>>
>> Using "createtree" method, i can find the first level (frameset), but nothing under this level. idem with "children" property.
>>
>>
>> Any idea ?
>>
>>
>> Laurent
>>
>> ----
>>
>> Tim Roberts, timr at probo.com <https://mail.python.org/mailman/listinfo/python-win32>:
>>
>> Why don't you show us the code you have? The Internet Explorer COM
>>
>> object surface is very large, so we don't want to waste time explaining
>> what you already know.
>>
>>
>> 2016-02-22 9:46 GMT+01:00 laurent solano <laurent.solano at gmail.com>:
>>
>>> Hi,
>>>
>>> I'm automating ie webbrowser with win32com module.
>>>
>>> I need to locate some xpath elements to manipulate them. Is there a way
>>> to do it ?
>>> More or less, i need the same feature than findElement(By.xpath ...)
>>> from selenium.
>>>
>>> Can anyone help ?
>>> thanks in advance,
>>>
>>> Laurent,
>>>
>>
>> _______________________________________________
>> python-win32 mailing list
>> python-win32 at python.org
>> https://mail.python.org/mailman/listinfo/python-win32
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20160309/2bfc26d4/attachment.html>
More information about the python-win32
mailing list