6

如何使用Webdriver Selenium从菜单中选择元素?菜单下拉菜单显示在鼠标悬停

4

4 回答 4

7

您可以通过两种方式检查它:1)第一种方法是使用操作生成器

WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).Click();
sbEle = driver.findElement(By.Id("sbEle")).Click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).Click();

这里

2)另一种方法是使用jscript直接点击需要的元素而不模拟鼠标悬停事件:

String cssLocatorOfTheElement=....//locator of the element to click on    
JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x = $(\'"+cssLocatorOfTheElement+"\');");
            stringBuilder.append("x.click();");
            js.executeScript(stringBuilder.toString());

希望这对你有用)

于 2012-09-05T15:49:32.940 回答
0

模拟 mouseOver 事件,然后选择你喜欢的元素:

var elementToShowMenu = Driver.FindElement(Byl.Id("some id")); 
new Actions(Driver).MoveToElement(elementToShowMenu).Release(elementToShowMenu).Build().Perform();
var menuElement = Driver.FindElement(Byl.Id("your menu id"));
于 2012-09-05T06:31:45.057 回答
0

以下是我如何单击标签上的不可见锚链接:由 Javascript 动态生成的链接:

public static void mouseClickByLocator( String cssLocator ) {
     String locator = cssLocator;
     WebElement el = driver.findElement( By.cssSelector( locator ) );
     Actions builder = new Actions(driver);
     builder.moveToElement( el ).click( el );
     builder.perform();
}
于 2012-12-18T17:47:11.057 回答
0
WebElement mnuElement;
WebElement submnuElement;
mnuElement = driver.findElement(By.cssSelector("insert selector here"));
submnuElement = driver.findElement(By.cssSelector("insert selector here"));

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuElement).perform();
// Pause 2 Seconds for submenu to be displayed
TimeUnit.SECONDS.sleep(2); //Pause 2 seconds
// Clicking on the Hidden submnuElement
submnuElement.click();  
于 2017-03-13T13:53:28.120 回答