下拉菜单中找不到元素

时间:2016-05-03 20:30:10

标签: selenium-webdriver

enter image description here我已尝试过所有选择器甚至动作构建器。但似乎没有任何效果。

我正在尝试在Chrome上运行它

我没有这样的元素异常

  • 加载Baubleabar
  • 添加任何项目
  • 点击购物车
  • 点击查看购物袋(从下拉列表)

...谢谢

这是HTML

 <li class="_JS_UserLoggedOut header_subNav_listItem" style="display:inline-    block">
    <li class="cartDropdown_container _JS_cartWidget header_subNav_listItem">
    <a class="header_subNav_link header_subNav_cartIcon" href="/checkout/cart/">
    <span class="cart-qty-indicator _JS_cartQty">1</span>
    <div class="cartDropdown __showCartWidget">
    <button class="dropdown-cart-scrollup btn_reset _JS_crt_up" style="display: none;">
    <ul class="_JS_scrollUI cart-item-container _JS_cartItemsContainer" data-item-count="" data-scrollable="false">
    <div class="cartDropdown_emptyMsg">
    <button class="dropdown-cart-scrolldown btn_reset _JS_crt_down" style="display: none;">
   <div class="cartDropdown_subtotal group">
   <a class="btn_highlight cartDropdown_viewCartLink" href="/checkout/cart/">
   </div>
   </li>

2 个答案:

答案 0 :(得分:0)

请您能告诉我们您要选择的HTML代码和您的webdriver代码吗?没有这些,很难知道问题的原因。

在C#中使用Selenium,有一种方法可以帮助您:它将匹配选项的值,名称或ID

public void SelectIn(By by, string value)
{
    var dropDownListBox = Driver.FindElement(by);
    var clickThis = new SelectElement(dropDownListBox);

    clickThis.Options.First(o =>
        o.Text.ToLower() == value.ToLower() 
        || o.GetAttribute("value").ToLower() == value.ToLower()
        || o.GetAttribute("id").ToLower() == value.ToLower())
    .Click();
}

答案 1 :(得分:0)

尝试以下代码:

Thread.sleep(2000);//don't use this, use explicit wait.

 //click on shopping bag at top
 driver.findElement(By.cssSelector(".header_subNav_link.header_subNav_cartIcon")).click();
 Thread.sleep(2000);

//if u want to get total number of items from here, use it
List<WebElement> elem = driver.findElements(By.cssSelector("#cart-items>li.cart-item"));
System.out.println("total product in ur bag is "+elem.size());

  // click on view shopping bag. 
  driver.findElement(By.cssSelector(".shopping-cart-icon-small")).click();
  Thread.sleep(2000);

//get total number of product from here
List<WebElement> elems = driver.findElements(By.cssSelector("article.cart_checkoutReview_item"));
System.out.println("total product in ur bag is "+elems.size());

让我知道会发生什么。 你可以使用javascript点击

  WebElement element =     driver.findElement(By.cssSelector(".header_subNav_link.header_subNav_cartIcon"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element);