繁体   English   中英

使用Selenium Webdriver的鼠标悬停功能

[英]Mouse hover functionality using selenium Webdriver

我想将鼠标悬停在图像图标上,然后显示下拉列表,稍后要从下拉列表中单击第一个选项。

我尝试了所有这些选项,但没有一个适合我。 请建议

    Actions act = new Actions(driver);
    WebElement iconhover =driver.findElement(By.className("insertItems"));
    act.moveToElement(iconhover).click().build().perform();
    WebElement ModulesAndTopics = driver.findElement(By.xpath("//*[@title='Topics']"));
    ModulesAndTopics.click();

再试一次

driver.switchTo().window(subwindow);
WebElement element = driver.findElement(By.className("insertItems"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());

您需要将鼠标悬停在下拉列表上并单击。

Actions act = new Actions(driver);
WebElement iconhover =driver.findElement(By.className("insertItems"));
act.moveToElement(iconhover).click().build().perform();
WebElement ModulesAndTopics = driver.findElement(By.xpath("//*[@title='Topics']"));
act.moveToElement(ModulesAndTopics).click().build().perform();   //hover and click

请尝试此操作,如果不能解决您的问题,请向我提供您的HTML,以便我检查:

new Actions(driver).
    moveToElement(driver.findElement(By.className("insertItems")))
      .build().perform();
    Thread.sleep(2000);
    //If //*[@title='Topics'] is xpath of your dropdown select tag
    Select sel=new Select(wd.findElement(By.xpath("By.xpath("//*[@title='Topics']")")));
    sel.selectByIndex(Index_OF_Option);

我认为您正在处理菜单项。 我假设className =“ insertItems”是唯一的。

//Locate the image icon
WebElement iconhover = driver.findElement(By.className("insertItems"));

//Hover mouse above the image icon - **DONT click**
Actions builder = new Actions(driver);
builder.moveToElement(iconhover).build().perform();

//Locating the first menu item - *plz use tagname instead of * in xpath* as its preferable
WebElement modulesAndTopics = driver.findElement(By.xpath("//*[@title='Topics']"));
modulesAndTopics.click();

请注意,有时下拉列表需要一些时间才能打开,因此建议在定位菜单项“ modulesAndTopics”时使用“显式等待”,如下所示:

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement modulesAndTopics = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@title='Topics']")));
modulesAndTopics.click();

请尝试以下代码,看来您还没有使用Select类:

WebElement iconhover = driver.findElement(By.className("insertItems"));       
Actions builder = new Actions(driver);    
builder.moveToElement(iconhover).build().perform();    
WebElement abc=driver.findElement(By.xpath("//*[@title='Topics']");    
Select drop= new Select(abc);    
drop.selectByVisibleText("xyz");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM