2016-11-30 99 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(); 
}