简体   繁体   中英

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 ).

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)

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.

Use below javascript syntax

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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