2016-11-30 72 views
1

我无法点击列表中的某个元素,一旦我点击了下拉列表?Webdriver等待TEXT出现

我创建的方法不起作用。

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    WebElement textToClick = driver.findElement(By.linkText(textToAppear)); 
    wait.until(ExpectedConditions.elementToBeClickable(textToClick)); 
    textToClick.click(); 
} 

使用thread.sleep似乎工作,但我不想使用此方法,任何人都可以推荐一个方法来等待和特定的文本元素上单击一次我点击了主键?

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    Thread.sleep(3000); 
    driver.findElement(By.linkText(textToAppear)).click();; 

} 

请注意我需要点击BBQ酱,需要点击烧烤酱当thread.sleep()成功

enter image description here

感谢您的帮助

+0

可以还添加了异常跟踪供我们参考? –

+1

使用现有的ExpectedConditions,如 - \t textToBePresentInElement或\t textToBePresentInElementValue。检查API - \t textToBePresentInElement。 – Grasshopper

+0

请发布相关的HTML。 – JeffC

回答

0

使用自己的执行FluentWait以等待文本出现在您的点击后:

Wait wait = new FluentWait<>(this.driver) 
    .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS) 
    .pollingEvery(500, TimeUnit.MILLISECONDS) 
    .ignoring(StaleElementReferenceException.class) 
    .ignoring(NoSuchElementException.class) 
    .ignoring(ElementNotVisibleException.class); 

WebElement foo = wait.until(new Function() { 
    public WebElement apply(WebDriver driver) { 
    return element.getText().length() > 0; 
    } 
}); 
0

您的帮助下面的方法似乎都得益于做的伎俩:

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    wait.until(ExpectedConditions.elementToBeClickable(By.linkText(textToAppear))); 
    driver.findElement(By.linkText(textToAppear)).click(); 
}