无法使用带有 Selenium Webdriver 的 AutoIT 以所需 location/folder 保存图像
Not able to save image at desired location/folder using AutoIT with Selenium Webdriver
我正在尝试使用 AutoIT(以控制 OS 弹出 Window)和 Selenium Webdriver(从我尝试下载图片的位置打开网站)从网站下载图像).
我收到了 OS 弹出窗口 window,通过使用 AutoIT,我能够发送用于保存文件的新位置 i.e,
C:\Users\Casper\Desktop\Resume\Pic.jpg
但是一旦脚本点击保存按钮,图片就会下载,但名称不同并且位于 different/default 位置。
我正在使用的 AutoIT 脚本写在下面-
WinWait("Save As");
WinActive("Save As");
Sleep(1000);
ControlSetText("Save As","","[CLASS:Edit; INSTANCE:1]","C:\Users\Casper\Desktop\Resume\Pic.jpg");
Sleep(1000);
ControlClick("Save As","","[CLASS:Button; INSTANCE:1]");
Sleep(1000);
Java Webdriver 代码-
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public void pic() throws AWTException, IOException, InterruptedException{
WebDriver driver;
System.setProperty("webdriver.chrome.driver","E:\chromedriver.exe");
driver = new ChromeDriver();
try{
driver.navigate().to("http://i.stack.imgur.com/rKZOx.jpg?s=128&g=1");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("/html/body/img"))).perform();
action.contextClick().perform();
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
// Here i am getting the os window but don't know how to send the desired location
String command ="C:\Users\Casper\Desktop\Resume\Pic.exe";
Runtime.getRuntime().exec(command);
}catch(Exception e){
e.printStackTrace();
driver.close();
}//catch
finally{
Thread.sleep(6000);
System.out.println("command");
driver.quit();
System.exit(0);
}
}//method
如您所见,它已成功将新地址发送到 OS Window Pop(红色圆圈内),但在单击“保存”按钮后,图像正在不同的位置下载 C:\Users\Casper\Downloads (my default download folder)
不同的名称 -rKZOx
也许可以试试这样:
Global $goExplorer = _myExplorerSelectUpload("C:\Users\Casper\Desktop\Resume", "Pic.exe", "Save As")
If @error Then Exit 101
ControlClick(HWnd($goExplorer.hwnd),"","[CLASS:Button; INSTANCE:1]")
Func _myExplorerSelectUpload($szDirectory, $szFileName, $vWndOrTitle, $sText = "")
Local $oExplorer = _explorerWinFindObj($vWndOrTitle, $sText)
If @error Then Return SetError(@error, @extended, 0)
$oExplorer.Navigate($szDirectory)
If @error Then Return SetError(3, 0, 0)
; might try a sleep here if it's rendering too fast
$oExplorer.document.SelectItem( _
$oExplorer.document.Folder.ParseName($szFileName), 1 + 4 + 8 + 16)
If @error Then Return SetError(5, 0, 0)
; return the object you're working with
Return $oExplorer
EndFunc
Func _explorerWinFindObj($vWndOrTitle, $sText = "")
Local $oShell = ObjCreate("Shell.Application")
If Not IsObj($oShell) Then
Return SetError(1, 0, 0)
EndIf
Local $oWins = $oShell.windows
Local $hWnd, $vDummy
For $oWin In $oWins
; browser confirmation - start
$vDummy = $oWin.type
If Not @error Then ContinueLoop ; if not/browser
$vDummy = $oWin.document.title
If Not @error Then ContinueLoop
; browser confirmation - end
; bypassed IE windows, now to find window
$hWnd = HWnd($oWin.hwnd)
If IsHWnd($vWndOrTitle) Then
; hwnd was passed, does it equal hwnd of object
If $hWnd = $vWndOrTitle Then Return $oWin
Else
; match titles (exact match)
If WinGetTitle($hWnd) = $vWndOrTitle Then
; match text, only in string text match
If $sText And Not _
StringInStr(WinGetText($hWnd), $sText) Then
ContinueLoop
EndIf
Return $oWin
; hwnd to hwnd
ElseIf WinGetHandle($vWndOrTitle, $sText) = $hWnd Then
Return $oWin
EndIf
EndIf
Next
Return SetError(2, 0, 0)
EndFunc
现在我得到了答案。由于我没有等待 window 正常打开,所以我无法将文件下载到我想要的位置。我只是让线程等待 2 秒,现在它工作正常并将图像保存在所需位置。更改后的代码是-
其余代码保持不变,以下代码已更改 -
Thread.wait(2000);
String command ="C:\Users\Casper\Desktop\Resume\Pic.exe";
Runtime.getRuntime().exec(command);
现在我可以将图片保存在e盘,文件名为pic
我正在尝试使用 AutoIT(以控制 OS 弹出 Window)和 Selenium Webdriver(从我尝试下载图片的位置打开网站)从网站下载图像).
我收到了 OS 弹出窗口 window,通过使用 AutoIT,我能够发送用于保存文件的新位置 i.e,
C:\Users\Casper\Desktop\Resume\Pic.jpg
但是一旦脚本点击保存按钮,图片就会下载,但名称不同并且位于 different/default 位置。
我正在使用的 AutoIT 脚本写在下面-
WinWait("Save As");
WinActive("Save As");
Sleep(1000);
ControlSetText("Save As","","[CLASS:Edit; INSTANCE:1]","C:\Users\Casper\Desktop\Resume\Pic.jpg");
Sleep(1000);
ControlClick("Save As","","[CLASS:Button; INSTANCE:1]");
Sleep(1000);
Java Webdriver 代码-
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public void pic() throws AWTException, IOException, InterruptedException{
WebDriver driver;
System.setProperty("webdriver.chrome.driver","E:\chromedriver.exe");
driver = new ChromeDriver();
try{
driver.navigate().to("http://i.stack.imgur.com/rKZOx.jpg?s=128&g=1");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("/html/body/img"))).perform();
action.contextClick().perform();
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
// Here i am getting the os window but don't know how to send the desired location
String command ="C:\Users\Casper\Desktop\Resume\Pic.exe";
Runtime.getRuntime().exec(command);
}catch(Exception e){
e.printStackTrace();
driver.close();
}//catch
finally{
Thread.sleep(6000);
System.out.println("command");
driver.quit();
System.exit(0);
}
}//method
如您所见,它已成功将新地址发送到 OS Window Pop(红色圆圈内),但在单击“保存”按钮后,图像正在不同的位置下载 C:\Users\Casper\Downloads (my default download folder)
不同的名称 -rKZOx
也许可以试试这样:
Global $goExplorer = _myExplorerSelectUpload("C:\Users\Casper\Desktop\Resume", "Pic.exe", "Save As")
If @error Then Exit 101
ControlClick(HWnd($goExplorer.hwnd),"","[CLASS:Button; INSTANCE:1]")
Func _myExplorerSelectUpload($szDirectory, $szFileName, $vWndOrTitle, $sText = "")
Local $oExplorer = _explorerWinFindObj($vWndOrTitle, $sText)
If @error Then Return SetError(@error, @extended, 0)
$oExplorer.Navigate($szDirectory)
If @error Then Return SetError(3, 0, 0)
; might try a sleep here if it's rendering too fast
$oExplorer.document.SelectItem( _
$oExplorer.document.Folder.ParseName($szFileName), 1 + 4 + 8 + 16)
If @error Then Return SetError(5, 0, 0)
; return the object you're working with
Return $oExplorer
EndFunc
Func _explorerWinFindObj($vWndOrTitle, $sText = "")
Local $oShell = ObjCreate("Shell.Application")
If Not IsObj($oShell) Then
Return SetError(1, 0, 0)
EndIf
Local $oWins = $oShell.windows
Local $hWnd, $vDummy
For $oWin In $oWins
; browser confirmation - start
$vDummy = $oWin.type
If Not @error Then ContinueLoop ; if not/browser
$vDummy = $oWin.document.title
If Not @error Then ContinueLoop
; browser confirmation - end
; bypassed IE windows, now to find window
$hWnd = HWnd($oWin.hwnd)
If IsHWnd($vWndOrTitle) Then
; hwnd was passed, does it equal hwnd of object
If $hWnd = $vWndOrTitle Then Return $oWin
Else
; match titles (exact match)
If WinGetTitle($hWnd) = $vWndOrTitle Then
; match text, only in string text match
If $sText And Not _
StringInStr(WinGetText($hWnd), $sText) Then
ContinueLoop
EndIf
Return $oWin
; hwnd to hwnd
ElseIf WinGetHandle($vWndOrTitle, $sText) = $hWnd Then
Return $oWin
EndIf
EndIf
Next
Return SetError(2, 0, 0)
EndFunc
现在我得到了答案。由于我没有等待 window 正常打开,所以我无法将文件下载到我想要的位置。我只是让线程等待 2 秒,现在它工作正常并将图像保存在所需位置。更改后的代码是-
其余代码保持不变,以下代码已更改 -
Thread.wait(2000);
String command ="C:\Users\Casper\Desktop\Resume\Pic.exe";
Runtime.getRuntime().exec(command);
现在我可以将图片保存在e盘,文件名为pic