2016-07-22 21 views
0

我正在使用Selenium IDE。我嘗試搜索三星s3圖像,並右鍵單擊一個圖像,然後點擊保存圖像,然後窗口來了,我不知道如何點擊保存按鈕。 我的代碼是這樣的關於硒如何控制窗口彈出

 WebDriver driver = new FirefoxDriver(); 
    driver.get("http://www.google.com"); 

    driver.findElement(By.id("lst-ib")).clear(); 
    driver.findElement(By.id("lst-ib")).sendKeys("samsung s3 picture"); 
    driver.findElement(By.name("btnG")).click(); 
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
    driver.findElement(By.linkText("Images for samsung s3")).click(); 
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
    WebElement image=driver.findElement(By.name("JJJ3gKgkvtZE4M:")); 


    Actions action= new Actions(driver); 
    action.contextClick(image).build().perform(); 
    action.sendKeys(Keys.CONTROL, "v").build().perform(); 

     Robot robot = new Robot(); 

     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 
     robot.keyPress(KeyEvent.VK_DOWN); 
     robot.keyRelease(KeyEvent.VK_DOWN); 

     // To press Save button. 
     robot.keyPress(KeyEvent.VK_ENTER); 
     robot.keyRelease(KeyEvent.VK_ENTER); 


     Alert alt=driver.switchTo().alert(); 
     Thread.sleep(3000); 
     alt.accept(); 

我想點擊保存按鈕

+0

而不是右鍵單擊並保存圖像,爲什麼不找到網址並保存圖像? – Polyov

+0

你能告訴我我該怎麼做 – Waqar

+0

我不能;我相信你可以做一些研究並自行解決。 – Polyov

回答

0

同時節省圖像是由瀏覽器掛上由OS不顯示彈出。基於OS的彈出窗口不能使用硒自動化,因爲它僅與瀏覽器交互。您可以使用AutoIT工具來自動化基於Windows的彈出窗口。希望能幫助到你!

+0

你能告訴我如何使用AutoIT嗎? – Waqar

+0

查看本教程:[AutoIT](http://toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/)。希望能幫助到你。 – Harish

0

控制此類操作系統相關事件的最佳方法是使用Robot類。

 Robot robot = new Robot(); // Robot class throws AWT Exception 
     Thread.sleep(2000); // Thread.sleep throws InterruptedException 
     robot.keyPress(KeyEvent.VK_DOWN); // press arrow down key of keyboard to navigate and select Save radio button 

     Thread.sleep(2000); // sleep has only been used to showcase each event separately 
     robot.keyPress(KeyEvent.VK_TAB); 
     Thread.sleep(2000); 
     robot.keyPress(KeyEvent.VK_TAB); 
     Thread.sleep(2000); 
     robot.keyPress(KeyEvent.VK_TAB); 
     Thread.sleep(2000); 
     robot.keyPress(KeyEvent.VK_ENTER); 
    // press enter key of keyboard to perform above selected action 
0

您不需要使用機器人下載圖像! 有更多的優雅和快速的方式來做到這一點。 例如,你可以創建一個自定義Firefox的配置文件並基於MIME類型自動下載..

已經有噸的關於回答問題,例如:access-to-file-download-dialog-in-firefox

你也可以採取this看看值得一讀的鏈接。