【问题标题】:Selenium cannot click menu-item from bootstrap dropdownSelenium 无法从引导下拉菜单中单击菜单项
【发布时间】:2017-06-27 11:14:04
【问题描述】:
Actions action = new Actions(driver);
        WebElement we = driver.findElement(By.xpath("//*[@id='ctl00_Sitemap1_HyperLink1']"));
        action.moveToElement(we).build().perform();
        WebElement tmpElement= driver.findElement(By.xpath("//*[@id='ctl00_Sitemap1_HyperLink1']"));
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", tmpElement);
        List<WebElement> dd_list = driver.findElementsByXPath("//*[@id='masterNavigation']/ul/li[1]/ul/li"); 
           for (WebElement ele : dd_list) 
           {     
              System.out.println("Values " + ele.getAttribute("innerHTML"));     
              if (ele.getAttribute("innerHTML").contains("Event Dashboard")) {                   
                 ele.click();                                        
                 break;  
              } 
           }    
    }

您好,我正在尝试自动化引导下拉菜单。默认情况下它的可见性是隐藏的。一旦你将鼠标悬停在它上面,它的可见性属性就会显示可见。我可以点击下拉菜单,但是在点击下拉菜单后,我的 selenium 脚本没有从下拉菜单中选择值。

错误:线程“main”中的异常 org.openqa.selenium.ElementNotVisibleException:无法点击 元素

HTML 代码片段

<a class="ui-button-text-icons" id="ctl00_Sitemap1_HyperLink1" href="javascript:void(void);">
    <span style="padding-right: 1.3em;">Dashboards</span>
    <span class="ui-button-icon-secondary ui-icon ui-icon-triangle-1-s"></span>
</a>
<ul style="visibility: hidden;">
    <li class="first featureGranted">
        <a href="ClassicDashboard.aspx">Classic Dashboard</a>
    </li>
</ul>

【问题讨论】:

  • HTML 代码:'仪表板 '
  • 请格式化您问题中的 HTML
  • @NehaDixit 正如你提到的visibility is hidden by default 和鼠标悬停visibility property shows visible 这样你就可以点击下拉菜单,现在你的脚本要从下拉菜单中选择一个值,你必须阅读DropDown 元素的属性并相应地选择它们。如果它导致ElementNotVisibleException,你必须放慢一点。谢谢

标签: selenium selenium-webdriver webdriver


【解决方案1】:

几件事

  • 您无需遍历所有 li 元素即可找到您想要的元素,您可以使用 Xpath 完成
  • 我不知道您为什么要使用 JavaScript 单击第一个元素,但除非 Selenium 提供的单击方法不起作用,否则我建议不要使用 JavaScript Click
  • 错误提示该元素不可见,这可能是由于多种原因。您可以使用显式等待直到元素可见,如下所述。它可能会解决您的问题

代码

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[@id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[@id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// I have no idea why you are clicking using JavaScript
js.executeScript("arguments[0].click();", tmpElement);

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement eventDashboardMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'Event Dashboard')]")));
eventDashboardMenu.click();

【讨论】:

  • 上述解决方案似乎对我有用。但不总是。它工作了2-3次,大多数情况下没有。你有解决这个问题的办法吗?
  • 20 秒时间:线程“主”org.openqa.selenium.TimeoutException 中的异常:等待 By.xpath 定位的元素可见性 20 秒后超时。 30秒时间。出现同样的错误
  • 实际上可能需要超过 30 秒。你可以放 try/catch 并截屏并检查元素是否真的可见?
猜你喜欢
  • 2023-03-31
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 2019-04-18
  • 2015-08-08
相关资源
最近更新 更多