如何通过Selenium-Webdriver和Java使用Java进行鼠标悬停

如何通过Selenium-Webdriver和Java使用Java进行鼠标悬停

问题描述:

尝试自动执行门户 http://demo.nopcommerce.com/时,无法执行以将鼠标悬停在电子设备"菜单上,然后选择相机和照片"子菜单. 使用以下脚本进行相同操作.

While trying to automate the portal http://demo.nopcommerce.com/, am not able to select mouse hover over "Electornics" menu and select "Camera & Photo" sub menu. Used the below script for the same.

WebElement electronic_Pdts = driver.findElement(By.xpath("//*[@class='title']//*[@title='Show products in category Electronics']"));
action.moveToElement(electronic_Pdts).build().perform();
driver.findElement(By.xpath("//*[@src='http://demo.nopcommerce.com/images/thumbs/0000006_camera-photo_450.jpeg']")).click();

通过 "Electornics" 菜单转到 Mouse Hover ,然后选择 "Camera & Photo" 您可以使用以下代码块:

To Mouse Hover over "Electornics" menu and select "Camera & Photo" you can use the following code block :

driver.get("http://demo.nopcommerce.com/");
Actions act = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement electronics = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li/a[@href='/electronics']")));
act.moveToElement(electronics).perform();
WebElement camera_n_photo = driver.findElement(By.xpath("//li/a[@href='/electronics']//following::ul/li/a"));
camera_n_photo.click();
System.out.println("Camera & photo Clicked.");