2017-01-15 5 views
0

メインメニューの上にマウスを置いて、Java Seleniumを使ってサブメニューを選択しようとしていますが、メニュー上にマウスを置いてもサブメニューを選択できません。私はxpathを使用する場合は、ビルドが成功したと言うが、新しいページを開くことはありません。これまでのコードはここまでですメニューにマウスを合わせてJava Seleniumのサブメニューを選択

System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe");  
    WebDriver webDriver = new ChromeDriver();  
    webDriver.manage().window().maximize(); 
    webDriver.navigate().to("https://www.skiutah.com"); 

    String NavTo = "DEALS"; 
    String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ; 
    WebElement element = webDriver.findElement(By.linkText(NavTo)); 
    WebElement el = webDriver.findElement(By.xpath(pathx)); 
    Actions action = new Actions(webDriver); 
    action.moveToElement(element).perform(); 
    action.moveToElement(el).click(); 

答えて

0

私はあなたがホバリングしてサブメニューをクリックする方法が正しくないようだと思います。

htmlを共有していないため、探している要素をチェックするのがちょっと面倒です。すべての罰金場合は、あなたを助けるかもしれないコードを、次の試してみてください -

WebElement menu = driver.findElement(By.your_locator); 
WebElement sub_menu = driver.findElement(By.your_locator); 
Actions action = new Actions(driver); 
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform(); 

、について説明: -

ここbuild()方法は、単一のステップと実行される準備ができにアクションのすべてのリストをコンパイルするために使用され

1

WebDriverでは、Mouseイベントを制御するオプションを指定しました。このコードを試してみてください。これはその目的に役立つはずです。

driver.get("https://www.skiutah.com/"); 
WebElement deals = driver.findElement(By.xpath("//a[@title='Deals']")); 
Mouse mouse = ((HasInputDevices) driver).getMouse(); 
Locatable hoverItem = (Locatable) deals; 
mouse.mouseMove(hoverItem.getCoordinates()); 
WebElement beginner = driver.findElement(By.xpath("//a[text()='Beginner']")); 
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(beginner)); 
Locatable clickItem = (Locatable) beginner; 
mouse.mouseDown(clickItem.getCoordinates()); 
mouse.mouseUp(clickItem.getCoordinates()); 
System.out.println(driver.getTitle()); 
1

マウスオーバーアクションを使用するには、build.performを使用する必要があります。行動連鎖と呼ばれ、アクション連鎖が最後に一緒に実行されるようにします。または、次のように行を入れ替えることができます。私はよく見ました。メインメニューへ

String NavTo = "DEALS"; 
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ; 
WebElement element = webDriver.findElement(By.linkText(NavTo)); 
WebElement el = webDriver.findElement(By.xpath(pathx)); 
Actions action = new Actions(webDriver); 
action.moveToElement(el).click(); 
String NavTo = "DEALS"; 
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ; 
WebElement element = webDriver.findElement(By.linkText(NavTo)); 
WebElement el = webDriver.findElement(By.xpath(pathx)); 
Actions action = new Actions(webDriver); 
action.moveToElement(el).click(); 
action.moveToElement(element).perform(); 
0
//locate the menu to hover over using its xpath 
WebElement menu = driver.findElement(By.linkText("Deals")); 
//Initiate mouse action using Actions class 
Actions builder = new Actions(driver); 
// move the mouse to the earlier identified menu option 
builder.moveToElement(menu).build().perform(); 
// wait for max of 5 seconds before proceeding. 
// until this submenu is found 
WebDriverWait wait = new WebDriverWait(driver, 5); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a"))); 
//identify menu option from the resulting menu display and click 
WebElement menuOption = driver.findElement(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a")); 
menuOption.click(); 
0
This works for me first time, but if repeated for other menu item then it cant find or something. 

WebElement menu = driver.findElement(By.your_locator); 
WebElement sub_menu = driver.findElement(By.your_locator); 
Actions action = new Actions(driver); 
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform(); 
0

まずMousehover、その後、サブメニューのいずれかをクリックします。

WebDriverWait Wait = new WebDriverWait(driver,10); 
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[@id='top_menu']/ul/li[4]/a")))); 
Actions mousehover = new Actions(driver); 
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform(); 
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals")))); 
driver.findElement(By.linkText("All Deals")).click(); 
0

私はサブメニューをクリックしようとしています。メインメニューにマウスを重ねる必要があり、サブメニューをロードするのに数秒かかります。私はサブメニューを見つけてそれをクリックする必要があります。 は、ここで私は

Actions ac = new Actions(dr); 
WebElement we = dr.findElement(By.xpath(".//*[@id='ddtopmenubar']/ul/li[1]/a")); 
ac.moveToElement(we).build().perform(); 
WebDriverWait wait = new WebDriverWait(dr, 5); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a"))); 
WebElement e= dr.findElement(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a")); 
e.click(); 

を使用したコードですが、動作するように思われません。

として例外を取得: org.openqa.selenium.WebDriverException:私は、デバッグで同じことをしたときに '不明'

:バージョン: '不明'、リビジョン: '不明'、時間performActions が情報を構築しますモードでは、私はサブメニューをクリックすることができます。

+0

でもこのコードを試しました。WebElement menu = driver.findElement(By.your_locator); WebElement sub_menu = driver.findElement(By.your_locator); アクションaction = newアクション(ドライバ); action.moveToElement(メニュー).moveToElement(sub_menu).click()。build()。perform(); –

関連する問題