I have a python script that tries to upload a file from my PC to a web application.
I press via WebDriver the specific upload button in the browser and then a Win7 explorer window opens for me to navigate and select the desired file to upload.
How could I manipulate this window with pywinauto?
optional: could this be done in linux as well (with an appropriate library I suppose) ?
This is my sample code:
wd.find_element_by_css_selector("img.editLecturesButtons.fromVideo").click()
#switch to the lightbox
wd.switch_to_frame(int("1"))
#hit upload
wd.find_element_by_xpath("//*[#id='fileUpload']").click()
#TODO
import os,pywinauto.application
file = os.path.normpath("C:\Users\me\Desktop\image.jpg")
....
I agree with Mark, you should try the Webdriver methods. Regard to pywinauto, code may looks like:
import pywinauto
pwa_app = pywinauto.application.Application()
w_handle = pywinauto.findwindows.find_windows(title=u'Open', class_name='#32770')[0]
window = pwa_app.window_(handle=w_handle)
ctrl = window['Name']
ctrl.SetText(file)
ctrl = window['OK']
ctrl.Click()
This sollution only for Windows, since pywinauto uses win32 api.
Related
I m making an Excel automation via pywinauto library. But there is a hard challange for me due to using Excel Oracle add-ins called Smartview.
I need to click 'Private Connections' button, however i can't find any little info in app.Excel.print_control_identifiers() Private Connections
So i tried to use inspector.exe for find ui element regarding private connections button, however i couldn't find any little solvetion inside of inspector.exe's result inspector's result
Then i used another program called UISpy, however i can only find private connection's pane inside of the program. UISpy's result
i tried to find an answer but i couldn't find out anything. So, can you help me to click here?
By the way here is my code :
import pywinauto
from pywinauto import application
from pywinauto.keyboard import send_keys
from pywinauto.controls.common_controls import TreeViewWrapper
program_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
file_path = r"C:\Users\AytugMeteBeder\Desktop\deneme.xlsx"
app = application.Application(backend="uia").start(r'{} "{}"'.format(program_path, file_path))
# sapp = application.Application(backend="uia").connect(title = 'deneme.xlsx - Excel')
time.sleep(7)
myExcel = app.denemeExcel.child_window(title="Smart View", control_type="TabItem").wrapper_object()
myExcel.click_input()
Panel = app.denemeExcel.child_window(title="Panel", control_type="Button").wrapper_object()
Panel.click_input()
time.sleep(1)
app.denemeExcel.print_control_identifiers()
I'm writing a blender python plugin which has an integrated connection to an online market, when the user buys a product (3d model) a window needs to open up to make the transaction through PayPal. The code I'm using works ok for that:
import webbrowser
webbrowser.open_new("http://www.paypal.com/buyProductURL")
However the window does not open up in the center of the screen and I also cannot find out how to define the size of the browser window that is being opened.
How can I move the browser window to the center and how can I define its size?
I think this might be what you want:
import webbrowser
browser = ' '.join([
"chrome",
"--chrome-frame",
"--window-size=800,600",
"--window-position=240,80",
"--app=%s &",
])
webbrowser.get(browser).open('http://www.paypal.com/buyProductURL')
The %s parameter is the url used by the module. It's required to call the browser as a command with arguments.
Here are some answers on how to call chrome in the command line.
If you require portability with the same behavior across all browsers it seems to be unattainable to me. I don't think you have an alternative here unless you ship the browser with your code or write more code for browser alternatives like chrome, firefox and opera. I wrote a code where it would work as a popup-like on chrome and chromium and as a tab open on other browsers:
import webbrowser
try_browsers = {
"chrome": [
"chrome",
"--chrome-frame",
"--window-size=800,600",
"--window-position=240,80",
"--app=%s &",
],
}
try_browsers['chromium'] = try_browsers['chrome'][:]
try_browsers['chromium'][0] = 'chromium'
URL = 'http://www.paypal.com/buyProductURL'
for browser in try_browsers.values():
if webbrowser.get(' '.join(browser)).open(URL):
break
else:
webbrowser.open(URL)
From Selenium, I managed to automate my tasks for a website.
However I ran into a problem:I need to upload a file to my web-page before submitting the form.
It is NOT an option to write the uploaded file into it's input element, because it is more complicated than this.
So basically I need to launch the FileUpload dialog by clicking a button, sendKeys there, and then close it by clicking on Ok.
I am wondering if this is even possible using just Selenium?
I am using it from python (so I don't have access to Robot class)
I tried so far:
element.click()
time.sleep(5)
alert = driver.switch_to.alert
alert.send_keys("path.to.myfile.txt")
alert.accept()
(nothing happens - I mean, the file open dialog works fine, but it does not send the keys )
I also tried:
alert = driver.switch_to.alert
buildu = ActionChains(driver).send_keys('path.to.my.file.txt')
buildu.perform()
(also not working)
Maybe I am looking at it wrong...
Maybe the alerts is not a good approach?
Do you have any idea?
I would prefere not having to use AUTOIT (for my own reasons)
So my goal is to click on a link element (DONE) then the link opens the File Upload open file dialog (DONE), then I need to be able to enter a text in the only textBox of the new window, and click on the Ok button
EDIT
This is the Open File dialog that shows up.
All I want to do is send the filename directly to the window (the textBox is focused when the dialog shows up, so no need to make more actions). After I send the keys (text) I need to be able to click on the Open button
You can create a Java program that will paste the filename and press the enter key. I had this same problem exactly. This is how I implemented:
package myaots_basic;
import java.io.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class trial {
public static void main(String[] args) throws AWTException {
System.out.println("HELLO WORLD");
StringSelection attach1 = new StringSelection ("C:\\My Office Documents\\Selinium projects\\Data\\attachment1.doc");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(attach1, null);
Robot rb1 = new Robot();
rb1.delay(3000);
rb1.keyPress(KeyEvent.VK_CONTROL);
rb1.keyPress(KeyEvent.VK_V);
rb1.keyRelease(KeyEvent.VK_V);
rb1.keyRelease(KeyEvent.VK_CONTROL);
rb1.delay(500);
rb1.keyPress(KeyEvent.VK_ENTER); // press Enter
rb1.keyRelease(KeyEvent.VK_ENTER);
}
}
Name it to trial.jar. Make this class a an executable.
Then in your python code just add a simple step:
import subprocess
subprocess.call("java -jar trial.jar", shell=True)
I don't know if this is restricted to windows 10 or works slightly different in other Windows versions but you can use the following code/idea.
Open the windows shell with win32com.client.Dispatch("WScript.Shell")
Send Tab keys to navigate through the open file dialog
Paste your absolute file path into the top of the dialog and also into the file selection text field and send enter key when
tab-navigating to the "open" button
Make a sleep of at least 1 second between each action.. otherwise Windows blocks this action.
import win32com.client as comclt
...
def handle_upload_file_dialog(self, file_path):
sleep = 1
windowsShell = comclt.Dispatch("WScript.Shell")
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys("{ENTER}")
time.sleep(sleep)
windowsShell.SendKeys(file_path)
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys(file_path)
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys("{ENTER}")
...
I tried to open a pdf file and save it as xml1.0 using pywinauto. i have started writting the below code but i am not able to find the controls for menu and saving it as xml. i am new to pywinauto. can you help me in this. and also please suggest where can i get the tutorial for python pywinauto.
from pywinauto import application
In_File = "sample.pdf"
Ap = "C:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe"
app = application.Application()
app.start_(Ap)
app.
Thanks
Here is an example for Adobe Reader X
import pywinauto
pwa_app = pywinauto.application.Application()
w_handle = pywinauto.findwindows.find_windows(title=u'Adobe Reader', class_name='AcrobatSDIWindow')[0]
window = pwa_app.window_(handle=w_handle)
window.MenuItem(u'&File->#0').Click()
By the way am the author of GUI tool for pywinauto - SWAPY. It can generate some code.
I hope it would help your automation.
I wanted to automate Adobe Reader menus,
from pywinauto import application
app = application.Application()
app.start_(r"C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe")
so after how to get complete windows title "Adobe Reader" & navigate to File menus
Check the documentation: http://pywinauto.googlecode.com/hg/pywinauto/docs/index.html
You can use MenuItems() to retrieve the menu items of a Dialog