2016-07-26 71 views
0

這是我用於將鼠標從「產品」移動到「查找ATM分支」的代碼(總共有5個網絡元素產品,在線申請,付款,銀行方式,查找ATM分支):如何在selenium webdriver(Java)中將鼠標從左到右的圖標懸停?

package Advance_SeleniumPackg; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.internal.Coordinates; 
import org.openqa.selenium.internal.Locatable; 
import org.openqa.selenium.support.events.EventFiringWebDriver; 
import org.openqa.selenium.support.events.internal.EventFiringMouse; 

public class Mouse_Movement { 

    public static void main(String[] args) throws InterruptedException { 

     WebDriver webdriver=new FirefoxDriver(); 
     EventFiringWebDriver driver=new EventFiringWebDriver(webdriver); 
     WebListener listener=new WebListener(); 
     driver.register(listener); 

     EventFiringMouse mouse=new EventFiringMouse(driver,listener); 
     driver.navigate().to("http://www.icicibank.com/"); 

     Thread.sleep(5000L); 
     Locatable hoverItem = (Locatable) driver.findElement(By.linkText("Products")); 

     Locatable hoverItem1 = (Locatable) driver.findElement(By.linkText("Find ATM Branch")); 

     Coordinates MyTestCoordinates = hoverItem.getCoordinates(); 
     Coordinates MyTestCoordinates1 = hoverItem1.getCoordinates(); 


     try{ 
      mouse.mouseMove(MyTestCoordinates); 
      mouse.mouseMove(MyTestCoordinates1); 

     }catch(Throwable t){ 

      System.out.println(t); 
     } 
     Thread.sleep(3000L); 

    } 

} 
+0

你是否運行過你的代碼?你得到了什麼? –

+0

是的,我運行了我的程序,但它在產品和Find ATM分支上盤旋。其實我正在尋找一個定義源和目的地後自行運行的程序。 – Samy

回答

0

請使用下面的代碼將光標從「產品」轉移到「查找ATM分公司」選項卡

package SamplePrograms; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class WebDriverSecondProgram { 

/** 
* @param args 
*/ 
public static void main(String[] args) throws Exception { 
    WebDriver driver = new FirefoxDriver(); 
    WebDriverWait wait = new WebDriverWait(driver, 100); 

    driver.get("http://www.icicibank.com"); 
    wait.until(ExpectedConditions.titleContains("Personal Banking")); 

    Actions hoverOps = new Actions(driver); 
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Products']"))).build().perform(); 
    Thread.sleep(2000); 
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Apply Online']"))).build().perform(); 
    Thread.sleep(2000); 
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Payments']"))).build().perform(); 
    Thread.sleep(2000); 
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Ways to Bank']"))).build().perform(); 
    Thread.sleep(2000); 
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Find ATM Branch']"))).build().perform(); 
    Thread.sleep(2000); 

    driver.quit(); 
} 
} 

希望這有助於。

+0

是的,它的工作很好..謝謝! – Samy

+0

@Samy,不客氣。你能把這個標記爲答案嗎? –