Java Selenium-link missing field error warning to the field

Sheikh Rahman :

If you visit the site https://www.cleartrip.com and without filling out the fields click on Search Flight button you will get error. I can capture the error using:

//small[contains(text(),'You missed this')]  

However I am not sure how to link this to the actual field. Can I do something like this //input[@id='From']/child::small?

DebanjanB :

To get the fields showing the error as You missed this on cleartrip.com you need to induce WebDriverWait for visibilityOfAllElementsLocatedBy() and then create a List using Java stream() and map() as follows:

  • Code Block:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class clearTrip_warning {
    
        public static void main(String[] args) {
    
        System.setProperty("webdriver.chrome.driver", "C:\\SeleniumUtilities\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("--disable-extensions");
        options.addArguments("disable-infobars");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.cleartrip.com/");
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#SearchBtn"))).click();
        List<String> myFields = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//small[contains(text(),'You missed this')]//preceding::input[2]"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
        System.out.println(myFields);
        }
    }
    
  • Console Output:

    [FromTag, ToTag, DepartDate]
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307607&siteId=1