【问题标题】:How to get value from a list which is not a drop down in selenium如何从不是硒下拉列表的列表中获取价值
【发布时间】:2019-05-14 03:41:13
【问题描述】:

<div class="Select form-control undefined Select--single is-searchable has-value"><input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-3--value"><div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div><div class="Select-input" style="display: inline-block;"><style>input#undefined::-ms-clear {display: none;}</style><input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode"><div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div>

我正在尝试从下拉列表中选择一个值,但它的 html 标记未选中。 我尝试了各种选择,但都没有奏效。

WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]"));
List<WebElement> options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]"));
options.get(0).click();

我想从下拉列表中选择新的值。[]

我在执行时遇到错误 - 元素无法滚动到视图中

以下是页面的 HTML 代码-

<div class="Select form-control undefined Select--single is-searchable has-value">
   <input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR">
   <div class="Select-control">
      <span class="Select-multi-value-wrapper" id="react-select-3--value">
         <div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div>
         <div class="Select-input" style="display: inline-block;">
            <style>input#undefined::-ms-clear {display: none;}</style>
            <input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode">
            <div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div>
         </div>
      </span>
      <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
   </div>
</div>

【问题讨论】:

  • 用 div 试过但没用
  • 可以分享 HTML 代码吗?获取 Web 元素列表中的所有内容,然后尝试单击其中一个 Web 元素是执行此操作的标准方法。
  • 我分享了html代码图片
  • 你能用纯文本分享吗,我们很容易复制HTML代码并给你正确的解决方案。无论如何,下拉菜单中有多少选项?

标签: java selenium selenium-webdriver


【解决方案1】:

由于您的下拉菜单是由 Div 和 span 标签组成的,因此 Select 类在 selenium 中不起作用。

你可以试试的代码:

List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
 if(element.getText().equals(" your value from drop down")){
    element.click();
}
}  

如果您还有其他问题,请告诉我。

【讨论】:

  • WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]")); List options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]")); options.get(0).click();
  • 我也尝试过使用 xpath,但仍然没有选择该值
  • 分享了 html 标签,请让我知道相同的解决方案...下拉菜单中有 2 个值 - 新的和二手的
  • WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]")); List options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]")); for(WebElement option : options) { if (option.getText().equals("New")) { option.click(); } } ...在上面执行时不会显示错误,也不会选择值
  • 首先您必须编写代码以单击下拉菜单,然后才能使用我的解决方案。
【解决方案2】:

由于您收到元素错误,因此无法滚动到视图中。 您应该尝试使用 JavascriptExecutor 进行滚动。

  1. 这种方式将允许您水平/垂直滚动页面 ((JavaScriptExecutor)driver).executeScript("scroll(0,200)") - 仅垂直滚动 200 像素

2 。将元素带入焦点

((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);

【讨论】:

  • 试过javascript - //JavascriptExecutor je = (JavascriptExecutor) driver; //WebElement element = driver.findElement(By.xpath("//span[contains(@class,'Select-value-label')]")); //je.executeScript("arguments[0].scrollIntoView(true);",element);....但是不能滚动查看错误
【解决方案3】:

由于选项 'new' 定义了 id 属性,因此使用 id 可以直接点击对象。 下面的代码应该可以工作,前提是已经点击了下拉按钮。

driver.findElement(By.id("react-select-3--value")).click();
driver.findElement(By.id("react-select-3--value-item")).click();

【讨论】:

  • 这只会点击下拉菜单,不会从列表中选择“新建”值
  • 编辑:我添加了代码以单击列表中的新值。请尝试告知。
  • 我给出以下错误 - 无法定位元素:#react\-select\-3\-\-value\-item
  • 经过多次尝试 - 以下代码将在下拉字段中键入 New - driver.findElement(By.xpath("/html/body/section/div/div[2]/section/div /from/div/div[3]/div[1]/div/div/div[2]/div/div/div/div/div/span[1]/div[2]/input")).sendKeys (“新”);
  • 但这仍然不允许使用回车键,就像我们按照手动步骤操作通常会选择 "New"-driver.findElement(By.xpath("//span[(@id=' react-select-10--value')]")).sendKeys(Keys.ENTER);
【解决方案4】:

感谢您的回复。 它适用于以下查询,即通过合并发送键和键。输入-

driver.findElement(By.id("react-select-3--value")).click();
driver.findElement(By.xpath("/html/body/section/div/div[2]/section/div/from/div/div[3]/div[1]/div/div/div[2]/div/div/div/div/div/span[1]/div[2]/input")).sendKeys("New",Keys.ENTER);

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 2014-06-23
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多