简体   繁体   English

使用 Selenium Webdriver 单击 JQUERY 元素时出现问题

[英]Problems to click in a JQUERY element using Selenium Webdriver

I'm trying to click in some JQUERY elements from a very known website to practice Selenium ( http://the-internet.herokuapp.com/jqueryui/menu ).我正在尝试从一个非常知名的网站单击一些 JQUERY 元素来练习 Selenium ( http://the-internet.herokuapp.com/jqueryui/ )。

I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)我想出了如何导航到菜单(不确定我的代码是否是一个好的解决方案),但是无法单击最后一个子菜单选项(PDF、CSV、Excel)

I'm trying something like below:我正在尝试以下内容:

Actions builder = new Actions(driver);
Action mouseOverMenu;

mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option

mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option

String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");

//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");

Your JavaScript function of click is wrong.您的 JavaScript function 的点击是错误的。

Use below javascript syntax使用下面的 javascript 语法

executor.executeScript("arguments[0].click();", WebElement);

Below code works for me:下面的代码对我有用:

    Actions builder = new Actions(driver);
    Action mouseOverMenu;

    mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
    mouseOverMenu.perform(); //accessing Enabled menu option

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
    wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));

    mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
    mouseOverMenu.perform(); //accessing Downloads submenu option

    WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", webElement2);

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

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