I can launch the default browser (chrome) with
call WShell.Run("http://www.google.com", 1, false)
but if I try
call WShell.Run("http://www.google.com", 1, true)
I get an error:
"unable to wait for process"
How can I launch a browser (could be IE or chrome) in a new process and wait for that process to exit.
See this question for "why?"
2019. VBS using HP UFT (QTP)
In HP UFT, I found few ways to run the browser via VBS.
My favorite is SystemUtil.Run.
1.SystemUtil.Run
strURL = "www.google.com"
str_NavigateTo = "https://chesstempo.com/chess-tactics.html#5"
int_mode_Maximized = 3
SystemUtil.Run "iexplore.exe",strURL, , ,3
SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE", str_NavigateTo,"C:\Program Files\Internet Explorer", ,int_mode_Maximized
Where Mode & Description
'0 Hides the window and activates another window.
'1 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size
and position. Specify this flag when displaying the window for the
first time.
'2 Activates the window and displays it as a minimized window.
'3 - Activates the window and displays it as a maximized window.
'4 Displays the window in its most recent size and position. The active window remains active.
'5 Activates the window and displays it in its current size and position.
'6 Minimizes the specified window and activates
the next top-level window in the Z order.
'7 Displays the window as a minimized window. The active window remains active.
'8 Displays the window in its current state. The active window remains active.
'9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size
and position. Specify this flag when restoring a minimized window.
'10 Sets the show-state based on the state of the program that started the application.
*Extra detailed descriptions of the SystemUtil parameters can be found here:
SystemUtil.Run
2. InvokeApplication
InvokeApplication "C://Program Files/Internet Explorer/IEXPLORE.EXE http://www.wp.pl"
3. VBScript via WScript.shell
If the path to your executable contains spaces, use Chr(34) to ensure the path is contained within double quotes.
Dim oShellSet oShell = CreateObject ("Wscript.shell")'
'Example 1 - run a batch file:
oShell.run "F://jdk1.3.1/demo/jfc/SwingSet2.bat"
'Example 2 - run a Java jar file:
oShell.run "java -jar F://jdk1.3.1/demo/jfc/SwingSet2/SwingSet2.jar"
'Example 3 - launch Internet Explorer:
oShell.Run Chr(34) & "C://Program Files/Internet Explorer/IEXPLORE.EXE" & Chr(34)
Set oShell = Nothing
4. IE Automation Object Model
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate "http://www.google.com/"
oIE.Visible = True
......
Set oIE = Nothing
5. Use the Windows \ Start \ Run dialog.
Add the Windows Start button to the Object Repository using the “Add Objects” button in Object Repository dialog.
Open the Run dialog (Start -> Run), and learn the “Open” edit field and the “OK” button into the Object Repository.
Switch to the Expert View, and manually add the lines to open the Run dialog.
Example:
Window("Window").WinButton("Button").ClickWindow("Window").Type("R")
Manually enter the lines to enter the information to launch the application, and click the “OK” button of the Run dialog.
Example:
Dialog("Run").WinEdit("Open:").Type "C://Windows/System32/notepad.exe"
Dialog("Run").WinButton("OK").Click
WebUtil Object
In UFT 14.01 update, HPE introduced two new methods for WebUtil Object. LaunchBrowser and LaunchMobileBrowserWithID
WebUtil.LaunchBrowser Browser, [device_model, device_manufacturer, device_ostype, device_osversion]
WebUtil.LaunchBrowser "MOBILE_CHROME", "Apple_5s", "Apple", "IOS", "10.1.3"
WebUtil.LaunchMobileBrowserWithID Browser, device_ostype, device_id
WebUtil.LaunchMobileBrowserWithID "MOBILE_CHROME", "IOS", "02"
Source with more info: 6 ways to launch your application
I've found one way using --user-data-dir=/some/directory:
call Shell.Run("""%userprofile%\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"" --user-data-dir=/some/directory "http://www.google.com", 1, true)
I used this command to open the Google home page:
call Systemutil.Run("http:www.google.com, 1, true)
It opened Google home page without any errors.
Related
I want to automate task in my app using PywinAuto python lib. My goal is to click the open file button and load the file. I had managed to start app and open the file Open Dialog
however am unable to set filename to desire file path.
my code
app = application.Application(backend="uia")
app.start(MyAppPath).connect(title="MyApp",timeout = 5)
app.MyApp.wait('visible')
# click the open button
app.MyApp.Open.click()
app.MyApp.Open.print_control_identifiers()
# Open Dailogue identifiers dump : https://pastebin.com/nt5MpQJx
# tried to set file name , but not worked
app.Open.child_window(title="File name:", control_type="Text").Edit.SetText('C:\myfile.txt')
so any idea how to set the file name.
ok so i have fixed it by changing control_type="Text" to control_type="ComboBox" thought it should be Text but its ComboBox
working code
app = application.Application(backend="uia")
app.start(MyAppPath).connect(title="MyApp",timeout = 5)
app.MyApp.wait('visible')
# click the open button
app.MyApp.Open.click()
app.MyApp.Open.print_control_identifiers()
# Open Dailogue identifiers dump : https://pastebin.com/nt5MpQJx
# tried to set file name , but not worked
app.Open.child_window(title="File name:", control_type="ComboBox").Edit.SetText('C:\myfile.txt')
Object:
automate following process.
1. Open particular web page, fill the information in search box, submit.
2. from search results click on first result and download the PDF
Work done:
To reach to this object I have written a code as first step. The code works fine but opens up download pop up. Till the time I can't get rid of it, I can not automate the process further. Searched for very many solutions. But none has worked.
For instance, This solution is hard for me to understand and I think its more to do with Java then Python. I changed fire fox profile as suggested by many. This dose matches though not exactly same. I haven't tried as there is no much difference. Even this speaks about changing fire fox profile but that doesn't work.
My code is as below
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from time import sleep
import time
import wget
from wget import download
import os
#set firefox Profile
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
#set variable driver to open firefox
driver = webdriver.Firefox(profile)
#set variable webpage to open the expected URL
webpage = r"https://documents.un.org/prod/ods.nsf/home.xsp" # edit me
#set variable to enter in search box
searchterm = "A/HRC/41/23" # edit me
#open the webpage with get command
driver.get(webpage)
#find the element "symbol", insert data and click submit.
symbolBox = driver.find_element_by_id("view:_id1:_id2:txtSymbol")
symbolBox.send_keys(searchterm)
submit = driver.find_element_by_id("view:_id1:_id2:btnRefine")
submit.click()
#list of search results open up and 1st occarance is clicked by coppying its id element
downloadPage = driver.find_element_by_id("view:_id1:_id2:cbMain:_id135:rptResults:0:linkURL")
downloadPage.click()
#change windiows. with sleep time
window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
time.sleep(10)
driver.switch_to.window(window_after)
#the actual download of the pdf page
theDownload = driver.find_element_by_id("download")
theDownload.click()
Please guide.
The "Selections" popup is not a different window/tab, it's just an HTML popup. You can tell this because if you right click on the dialog, you will see the normal context menu. You just need to make your "Language" and "File type(s)" selections and click the "Download selected" button.
I wrote a macro on VBA using Selenium.ChromeDriver, which opens the site and gives it various actions.
But the problem is that if an error occurs in the macro and I click "Stop" in debugging mode, then the browser closes.
The problem is that when you close the browser and open it, you have to enter your login and password each time.
How can I avoid closing the browser opened via Chrome Driver if the macro ends or when an error occurs?
Did so
Public Driver as new ChromeDriver
but that doesn't help, the browser closes.
Need some more code really but some pointers
When error occurs click debug rather than end and the browser should remain open
Don't use .Quit if you want automated browser to remain open at end of sub. However, you will need to grab the session id if you wish to reconnect with that instance, or pass the driver object to any receiving sub. Just remember at some point to Quit.
Here is how you open another tab:
Option Explicit
'download selenium https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub DownloadFile()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "www.something/login/"
With d
.Start "Chrome"
.get URL
.FindElementById("name").SendKeys ""
.FindElementById("email").SendKeys ""
.FindElementByCss("[type=submit]").Click
Application.Wait Now + TimeSerial(0, 0, 5) '< better to have a wait condition for something on the post login page
.ExecuteScript "window.open(" & Chr$(34) & "postloginUrl" & Chr$(34) & ",'_blank');"
Stop
.Quit
End With
End Sub
I have a bunch of links in a list and I want to open each link in a different tab (only one window). I know how to open a new tab in Selenium but for some reason, when I iterate over the list, all links get open in the same tab and I don't know what I am missing. Could anyone explain me what the error is and how I can fix it? I would really appreciate it.
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys
url_list = ["https://www.kdnuggets.com/2017/06/text-clustering-unstructured-data.html", "https://github.com/vivekkalyanarangan30/Text-Clustering-API/", "https://machinelearningblogs.com/2017/01/26/text-clustering-get-quick-insights-from-unstructured-data/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/"]
driver = wd.Firefox(executable_path="/usr/local/bin/geckodriver")
for url in url_list:
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + "t")
driver.get(url)
Currently using python3.7, Firefox 65.0.1 and Selenium 3.141 on a Mac
When you open a new tab it is a new window for webdriver which will have its unique handle. driver.window_handles holds the list of active windows, you can use this to switch to newly created window and perform tasks on it.
for url in url_list:
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + "t")
driver.switch_to_window(driver.window_handles[-1])
driver.get(url)
Note that you will be using the same variable driver to refer to the newly switched window, so if you close that window then you need to switch to an active window again to perform further tasks.
UPDATE:
If new tab is not opening with your code then you can also try this.
for url in url_list:
driver.execute_script("window.open()")
driver.switch_to_window(driver.window_handles[-1])
driver.get(url)
use window switching with commands
one=driver.window_handles[0] - set the name of the first window
two=driver.window_handles[1] - the name of the second window (after opening it)
driver.switch_to.window(two) - switch to the desired window
Currently i'm using selenium 3.6 with IE web driver version as 3.4.
There is an area in my application where i have to click on a button which downloads an excel.When i do this in IE using selenium it opens a new window of IE (not happening when you do manually) along with normal download pop up.
When i tried to identify the other window which was opened, using window_handles method in python i'm not able to identify the other window as the window_handles is only identifying the main window.
Config details:
IE version 11.17
Windows 10
language :python 3.4
I had a similar problem with IE8. I had should do a switch to default content, before use the window_handles. That way identify more windows.
I know you are using Python, but the Java code for this is:
// Take the parent id before change
String parent = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
if (!winHandle.equals(parent)) {
driver.close();
}
}
driver.switchTo().window(parent);
I think the steps are the same on Python.