简体   繁体   English

如何使用 Selenium 或硒化物 select md-autocomplete 选项?

[英]How to select md-autocomplete option with Selenium or Selenide?

Here is my Java attempt, but what should I fix?这是我的 Java 尝试,但我应该修复什么?

SelenideElement product = $(By.id("product"));
Assertions.assertEquals("Search product", product.attr("md-floating-label"));
product.find(By.tagName("input")).val("test");
SelenideElement span = product.find(By.tagName("span")); //this one and last lines does not work as expected
product.sendKeys(Keys.ARROW_DOWN);
product.pressEnter();

And HTML code和 HTML 代码

<md-autocomplete id="product" flex="85" md-selected-item="ctrl.orderItem.product" md-search-text="ctrl.productQuery" md-items="item in ctrl.findProduct(ctrl.productQuery)" md-item-text="item.name" md-delay="300" md-floating-label="Search product">
  <div layout="row" class="item" layout-align="start center">
    <span md-highlight-text="ctrl.productQuery">{{item.name}}</span>
  </div>
</md-autocomplete>

Instead of doing product.find(By.tagName("span"));而不是做product.find(By.tagName("span")); I would try replacing it with an XPath as such:我会尝试用 XPath 替换它:

SelenideElement span = product.find(By.xpath("//*[@id='product']/div/span"));

The issue was that span is not a direct child element of product (which is a md-autocomplete element), so you need a slightly more complex locator to find the span .问题是span不是 product 的直接子元素(它是一个md-autocomplete元素),因此您需要一个稍微复杂的定位器来查找span

Maybe you dont need to do this thing Assertions.assertEquals("Search product", product.attr("md-floating-label"));也许你不需要做这件事Assertions.assertEquals("Search product", product.attr("md-floating-label"));

Just try find your span element like this SelenideElement span = $(By.xpath("//md-autocomplete[@id='product' and @md-floating-label= 'Search product"]//span')); span.sendKeys(Keys.ARROW_DOWN); span.pressEnter();只需尝试像这样找到您的跨度元素SelenideElement span = $(By.xpath("//md-autocomplete[@id='product' and @md-floating-label= 'Search product"]//span')); span.sendKeys(Keys.ARROW_DOWN); span.pressEnter(); SelenideElement span = $(By.xpath("//md-autocomplete[@id='product' and @md-floating-label= 'Search product"]//span')); span.sendKeys(Keys.ARROW_DOWN); span.pressEnter(); If element with label 'Search product' is not exist, you get a SoSuchElementException如果具有 label 'Search product' 的元素不存在,则会收到SoSuchElementException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM