【问题标题】:Selenium Webdriver javascript - pop up is getting open 4 times on every dropdown clickSelenium Webdriver javascript - 每次点击下拉菜单时弹出窗口打开 4 次
【发布时间】:2016-04-01 09:31:26
【问题描述】:

我是硒的新手。场景是,当我单击 popupid 时弹出窗口正在打开,弹出窗口中有 4 个下拉列表,我需要从每个下拉列表中选择值,但是在从每个下拉列表中选择值时,会打开新的弹出窗口,它会覆盖现有的.因此,在选择正确值方面也面临问题。

硒代码:

WebDriverWait waitFortab = new WebDriverWait(driver, 15);
waitFortab.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".node.settings.asettings.bookmgmt.undefined.ready")));
WebElement el5 = driver.findElement(By.cssSelector(".bookrouting")); // opens the page
js.executeScript("arguments[0].click();", el5);
WebElement el6 = driver.findElement(By.cssSelector(".ruleEnControls .fr.bookroute.addcategory")); // opens up popup
el6.click();
WebDriverWait waitForcategory = new WebDriverWait(driver, 15);
waitForcategory.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".popup.wrapper.bookroute_save.dffPopup")));
WebElement el8 = driver.findElement(By.cssSelector(".tag.dropdown.product_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el8);
WebElement el9 = driver.findElement(By.xpath("//*[text()='Vanilla (V)']")); // selects value from first pop up
js.executeScript("arguments[0].click();", el9); 

WebElement el10 = driver.findElement(By.cssSelector(".tag.dropdown.currency_pair_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el10);
WebElement el11 = driver.findElement(By.xpath("//*[text()='AUD/CAD']")); // selects value from second pop up
js.executeScript("arguments[0].click();", el11);

WebElement el12 = driver.findElement(By.cssSelector(".tag.dropdown.currency_brm .downArrow.closed"));
js.executeScript("arguments[0].click();", el12);
WebElement el13 = driver.findElement(By.xpath("//*[text()='AUD']"));  // selects value from third pop up
js.executeScript("arguments[0].click();", el13);

HTML 片段:

<ul class="dropdown_menu dn">
  <li class="option selectAllBR" data-value="" shortcut="" data-display="Select All" title="Select All">
  <div class="tag fl checkBoxClear ddselect checkbox r0 c0" irow="0" icol="0" tabindex="0" fieldtype="ddselect" tagtype="checkbox"><input type="hidden" value="0" class="toggleValue value"></div>
  <span class="oLayCheck"></span>Select All</li>
  <li class="option" data-value="euvanilla" shortcut="VANILLA (V)" data-display="Vanilla (V)" title="Vanilla (V)">
  <div class="tag fl checkBoxClear ddselect checkbox r0 c0" irow="0" icol="0" tabindex="0" fieldtype="ddselect" tagtype="checkbox"><input type="hidden" value="0" class="toggleValue value"></div>
  <span class="oLayCheck"></span>Vanilla (V)</li>
  <!-- there are several such <li> elements in each dropdown -->
</ul>

【问题讨论】:

    标签: java selenium xpath selenium-webdriver css-selectors


    【解决方案1】:

    我敢打赌,您在弹出窗口存在时遇到了一些时间问题,但在 DOM 上还没有准备好 - 所以点击发生在 previous 元素上。

    试试这样的:

    WebDriverWait wait = new WebDriverWait(driver, 15);
    WebElement el8 = driver.findElement(By.cssSelector(".tag.dropdown.product_brm .downArrow.closed"));
    wait.until(ExpexctedConditions.elementIsClickable(el8);
    js.executeScript("arguments[0].click();", el8);
    
    WebElement el9 = driver.findElement(By.xpath("//*[text()='Vanilla (V)']")); // selects value from first pop up
    wait.until(ExpexctedConditions.elementIsClickable(el9);
    js.executeScript("arguments[0].click();", el9); 
    

    几件事:

    1. 您不需要为不同的事情重新等待。只需重复使用现有的。
    2. 请注意,模式是
      • 找到元素
      • 等待元素可点击
      • 单击该元素。
    3. 另外,不确定您为什么要使用 JavaScriptExecutor。你可以简单地做“el9.click();”除非您的弹出窗口中的 JavaScript 有问题。

    HTH

    【讨论】:

    • 谢谢吉姆。它确实奏效了。我还从我的代码中删除了所有 JavaScriptExecutor 并使用了简单的点击。第 2 点非常有帮助
    • 很高兴它有帮助!通过第 2 点学会生活和呼吸。它为我节省了大量的悲伤。 (在我之前经历过巨大的悲伤之后。)
    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2014-04-30
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多