0

我有多个下拉列表,它们是在条件下动态创建的。Selenium webdriver不适用于使用java的多个动态下拉列表

我的Java代码::

开始 - >

WebElement eleOpt = driver.findElement(By.xpath("//*[@id='id_0_" + value1+ "'" + "]/div/div")); 
eleOpt.click(); 

Thread.sleep(200); 

WebElement clickSelectedEle = driver.findElement(By.xpath("//custom-select[@id='id_0_" + value1+ "'" + "]/div/div[2]/ul/li[" + Integer.parseInt(value2) + "]")); 
clickSelectedEle.click(); 

注:数值和value2都将在法传递的动态值。

自动2个下拉菜单越来越自动点击,但对于第三个它下面的错误投掷:

错误 -

org.openqa.selenium.NoSuchElementException: Unable to locate element: //custom-select[@id='id_0_2']/div/div[2]/ul/li[0]

HTML代码(角2):

<caption class="blind">{{a11y}} {{name}}.</caption> 

<div class="static" (click)="toggle()"> 
    <div class="selected"> 
     {{name}} 
    </div> 
</div> 

<div class="open"> 
    <ul> 
     <li id='{{option.code}}' (click)="changeval(option.code || option.id)" *ngFor="let option of options; let i = index" [ngClass]="{ 'active' : (selected && selected===i) }"> 
      <caption class="blind">option: {{option.name}}.</caption> 
      <span>{{option.name}}</span> 
     </li> 
    </ul> 
</div> 

请建议可以做些什么。

+0

添加相关的HTML。 – Guy

+0

很抱歉忘了把html代码。我编辑过。谢谢。 –

回答

0

一种选择是添加硒wait.until条件

WebDriverWait wait = new WebDriverWait(getDriver(), 10); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.css("YourCssPath"))); 

您也可以使用任何By.class,ID,...选项,我更喜欢使用By.class,或By.id在地方使用CSS或正则表达式,第一个选项是更快

+0

我明白了。但为什么它为2下拉和第三个投掷错误工作。我想知道这一点。 –

+0

可能对于一些竞争条件,如果你将睡眠时间改为15-20秒很容易 – cralfaro

0

200毫秒是真的少了时间。作为@cralffaro曾建议使用WebDriverWait或执行动作FluentWait

代码之前,您应该使用FluentWait

WebElement waitsss(WebDriver driver, By elementIdentifier){ 
    Wait<WebDriver> wait = 
       new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) 
               .pollingEvery(1, TimeUnit.SECONDS) 
               .ignoring(NoSuchElementException.class); 

     return wait.until(new Function<WebDriver, WebElement>() 
       { 
        public WebElement apply(WebDriver driver) { 
          return driver.findElement(elementIdentifier); 
        } 
       }); 
} 
相关问题