【问题标题】:Need help- Selenium webdriver- click on element from visible list is not working需要帮助-Selenium webdriver-单击可见列表中的元素不起作用
【发布时间】:2017-07-27 08:32:54
【问题描述】:

我正在从 excel 文件发送定义的数据。我尝试了一些代码,但他们没有在某个时间点从 excel 文件中选择所有数据,代码给了我未找到 WebElement 的异常。

这里是 HTML 代码:

<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
    <div id="addDialog" class="hidden ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 30px; height: auto; max-height: 351.05px; overflow-y: auto;">
        <div class="field-container">
        <fieldset class="field-container">
            <legend>Contracts:</legend>
            <a class="select-all" href="#">Select All</a>
            <a class="deselect-all" href="#">Deselect All</a>
            <select id="addContract" class="searchable" multiple="multiple" style="position: absolute; left: -9999px;">
                <option value="93370956">93370956</option>
                <option value="93796167">93796167</option>
                <option value="94203239">94203239</option>
            </select>
            <div id="ms-addContract" class="ms-container">
                <div class="ms-selectable">
                    <input class="search-input" type="text" placeholder="filter" autocomplete="off"/>
                        <ul class="ms-list" tabindex="-1">
                            <li id="86355560-selectable" class="ms-elem-selectable">
                                <span>93370956</span>
                            </li>
                            <li id="202890296-selectable" class="ms-elem-selectable">
                                <span>93796167</span>
                            </li>
                            <li id="938848030-selectable" class="ms-elem-selectable">
                                <span>94203239</span>
                            </li>
                        </ul>
                </div>
            </div> 

需要从列表中选择值。 所做的努力: 这种代码工作有效,但它只选择了一个值,然后给出了异常

WebDriverWait Wait=new WebDriverWait(driver, 10);
          Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[@class='ms-list']/li/span")));
          //now you can find element
          List<WebElement>options=driver.findElements(By.xpath("//ul[@class='ms-list']/li/span[contains(text(),'"+testData+"')]"));
          for (WebElement option: options) {
            if(testData.equals(option.getText())) option.click();
          }

试过上面的代码,但它只选择一个值!!

WebDriverWait Wait = new WebDriverWait(driver, 10);
        Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='ms-addContract']//descendant::div[@class='ms-selectable']/ul[@class='ms-list']]//span")));  

List<WebElement> options = driver.findElements(By.xpath("//*[@id='ms-addContract']//descendant::div[@class='ms-selectable']/ul[@class='ms-list']]//span[contains(text(), '"+testData+"')]"));
            for (WebElement option : options) {
              if(testData.equals(option.getText()))
                option.click();--tried this xpath-no success

WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ms-addContract']/div[1]/input"))).click();
    driver.findElement(By.xpath(".//*[@id='ms-addContract']/div[1]/input")).sendKeys(testData);
    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ms-addContract']/div[1]/input"))).sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.SPACE);
    //Clear the input text value.
    driver.findElement(By.xpath(".//*[@id='ms-addContract']/div[1]/input")).clear();---worked but not satisfactory

请帮助定位该元素。我正在使用关键字驱动的框架从 excel 文件中读取值。

【问题讨论】:

  • 哪些元素失败了?目前很难理解你的代码哪里出错了。
  • @MadisKangro 它没有从跨度类中选择值。当我在上面运行尝试过的代码时,他们只是停留在全选按钮上。
  • 添加堆栈跟踪 =)
  • 为此 xpath //ul[@class ='ms-list']/li/span[contains(text(),'95757314')]] 删除额外的 ]
  • @Tuks 。感谢您指出。但是一个问题是它只从值列表中选择了一个第一个值,它只是跳过了队列中的其他值。

标签: java selenium xpath selenium-webdriver dropdownlistfor


【解决方案1】:

听起来您正在尝试获取 ul 元素中包含的所有 span 元素(尽管如果我错了,请纠正我)。这是您正在使用的代码:

List<WebElement>options = driver.findElements(By.xpath("//ul[@class='ms-list']/li/span[contains(text(),'"+testData+"')]"));

在您的情况下,此 XPath 将仅返回 1 个值。这是因为您试图将每个跨度的内容与输入值“testData”进行匹配。如果要获取 ul 中的所有 span 元素,请使用:

List<WebElement>options = driver.findElements(By.xpath("//ul[@class='ms-list']/li/span"));

此 XPath 将 select 完成选择列表中的所有 span 元素。如果您只需要在 testData 中选择包含文本的 span 元素,那么您可以遍历该列表并选择适当的元素:

ArrayList<WebElement> optionsInTestData = new ArrayList<WebElement>();
for(WebElement element: options){
    for(String data: testData){
        if(element.getText() == data){
            optionsInTestData.add(element);
        }
    }
}

【讨论】:

  • 无法实例化类型 List & 只能遍历数组或 java.lang.Iterable 的实例,获取 List 和 for (String data: testData) 的这些错误。试图修改它。但它仍然无法正常工作。有 testData 列表,所以它应该一一选择它们。
  • 我有同样的情况,我用 List options = driver.findElements(By.xpath("//ul[@id='ui-id-2']/li" )); for (WebElement option : options) { if(testData.equals(option.getText())) option.click(); } 。它适用于该下拉列表。
  • 抱歉,我已经有一段时间没有使用 Java 编程了。我已将 optionsInTestData 更新为 ArrayList 而不是 List 对象。这应该让你初始化它。
  • testData 是什么对象类型?如果它是 List 您应该能够使用 foreach 循环。如果它仍然给你一个错误,你总是可以使用经典的 for(int i = 0; i
  • 尝试更改代码。它仍然没有进入循环。 testData 是你可以在 html 代码中看到的数字字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
相关资源
最近更新 更多