Java Selenium-link缺少字段错误警告字段

时间:2019-05-13 17:26:06

标签: java selenium webdriverwait

如果您访问网站https://www.cleartrip.com,但未填写字段,请单击“搜索排期”按钮,将出现错误。我可以使用以下方法捕获错误:

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

但是我不确定如何将其链接到实际字段。我可以做这样的事情//input[@id='From']/child::small吗?

1 个答案:

答案 0 :(得分:-1)

要获取显示错误的字段为您在cleartrip.com上错过了,您需要为visibilityOfAllElementsLocatedBy()引入 WebDriverWait ,然后创建一个< em> List 使用 Java stream()map(),如下所示:

  • 代码块:

    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);
        }
    }
    
  • 控制台输出:

    [FromTag, ToTag, DepartDate]