2016-09-01 8 views
0

以下のコードを試しました。デモ用ウェブサイトのマウス・ホバー・アクションがSelenium Webdriverで機能しない

public class FindingMultipleElements { 
    public static void main(String[] args) { 
     WebDriver driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
     driver.manage().window().maximize(); 
     driver.navigate().to("http://automationpractice.com/index.php"); 
     Actions act = new Actions(driver); 
     WebElement women = driver.findElement(By.xpath("//*[@id='block_top_menu']/ul/li[1]/a")); 
     //women.click(); 
     Point p1 = women.getLocation(); 
     int x = p1.getX(); 
     int y = p1.getY(); 
     System.out.println("X:"+x+" Y:"+y); 
     act.moveByOffset(x, y).click(driver.findElement(By.linkText("T-shirts"))).build().perform(); 

    } 
} 

enter image description here

私は女性のカテゴリ内の "Tシャツ" リンクをクリックする必要があります。マウスのホバーアクションを使用してリンクをクリックすることはできません。

答えて

2

以下のコードを使用します。

WebDriver driver = new FirefoxDriver(); 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
driver.manage().window().maximize(); 
driver.navigate().to("http://automationpractice.com/index.php"); 
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 

WebElement women = driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='Women']")); 

Actions builder = new Actions(driver); 
builder.moveToElement(women).perform();//this will hover to women 
Thread.sleep(1000);//avoid using this type of wait. wait using until. 

driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='T-shirts']")).click();//this will click on t-shirt 

が、これはあなたを助けることを願っています。

+0

はありません! – tsivarajan

0

あなたはすでにXYの座標を提供していますが、なぜdriver.findElement(By.linkText("T-shirts"))も提供していますか?それなしで試してみるとうまくいきます。

0

どの要素細かいおかげで作業By.linkText( "Tシャツ")

Actions act = new Actions(driver); 
WebElement womenLink = driver.findElement(By.xpath("//a[@title='Women']")); 
act.moveToElement(womenLink).click().build().perform(); 
WebElement tshirtLink = driver.findElement(By.xpath("//[@class='sfHoverForce']//a[@title='T-shirts']")); 
act.moveToElement(tshirtLink).click().build().perform(); 
関連する問題