gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:28:12 25 4
gpt4 key购买 nike

我正在尝试从一个非常知名的网站单击一些 JQUERY 元素来练习 Selenium ( http://the-internet.herokuapp.com/jqueryui/menu )。

我想出了如何导航到菜单(不确定我的代码是否是一个好的解决方案),但是我无法单击每个最后一个子菜单选项(PDF、CSV、Excel)

我正在尝试如下所示:

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

最佳答案

您的 JavaScript click 函数是错误的。

使用下面的 JavaScript 语法

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

下面的代码对我有用:

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

关于java - 使用 Selenium Webdriver 单击 JQUERY 元素时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com