2014-12-02 90 views
1

如何爲元素添加懸停?考慮我的下面的代碼:如何「懸停」硒中的按鈕?

package AutomationFramework; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.*; 

import org.openqa.selenium.firefox.FirefoxDriver; 

public class LoginPage { 

    private static WebDriver driver = null; 

    public static void main(String[] args) { 

     // Create a new instance of the Firefox driver 

     driver = new FirefoxDriver(); 

     //Put a Implicit wait 

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

     //Launch the Website 

     driver.get("URL"); 

     // Find the element that's ID attribute is 'account'(My Account) 

     // driver.findElement(By.xpath("/html/body/table/tbody/tr[5]/td/table/tbody/tr/td[2]/form/table/tbody/tr[3]/td[2]/input")).click(); 

     // Enter Username on the element 

     driver.findElement(By.name("userName")).sendKeys("remote"); 

     // Find the element that's ID attribute is 'pwd' (Password) 

     driver.findElement(By.name("password")).sendKeys("aaaaaa"); 

     // Now submit the form. WebDriver will find the form for us from the element 

     driver.findElement(By.name("submit")).click(); 

     // Print a Log In message to the screen 

     System.out.println(" Login Successfully"); 
     driver.findElement(By.name("img2")).click(); 

     // Find the element that's ID attribute is 'account_logout' (Log Out) 

     //driver.findElement (By.name("img104")).click(); 

     // Close the driver 

     driver.quit(); 

      } 

    } 
+0

你想要懸停的元素是什麼?請明確說明。 – Subh 2014-12-02 06:14:00

回答

2

繼續使用Action類。

Actions hover = new Actions(driver); 

WebElement Elem_to_hover = driver.findElementBy(By.id("id")); 

hover.moveToElement(Elem_to_hover); 

hover.build(); 

hover.perform(); 
1

Shruthi,請使用硒網絡驅動器參考下面的示例程序,鼠標懸停:

public class mhover { 

public static void main(String[] args){ 

    WebDriver driver = new FirefoxDriver(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    driver.get("http://www.google.com"); 
    WebElement ele = driver.findElement(By.id("gbqfba")); 
    Actions action = new Actions(driver); 
    action.moveToElement(ele).build().perform(); 


    } 

} 

請讓我知道如果您有任何疑問。

+0

謝謝你。它的工作很好 – 2014-12-04 06:48:34

+0

Great..enjoy ... – 2014-12-04 06:49:10