Java Selenium-link missing field 错误警告给字段

标签 java selenium webdriverwait

如果您访问网站 https://www.cleartrip.com如果不填写字段,请单击“搜索航类”按钮,您将收到错误消息。我可以使用以下方法捕获错误:

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

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

最佳答案

要在 cleartrip.com 上将错误显示为 You missed this您需要为 visibilityOfAllElementsLocatedBy() 引入 WebDriverWait,然后使用 Java stream() 创建一个 List 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]
    

关于Java Selenium-link missing field 错误警告给字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117172/

相关文章:

Java:序列化单例的实际用例?

python - selenium.common.exceptions.WebDriverException : Message: Can't load the profile

javascript - 如何使用 Selenium IDE 测试损坏的图像?

maven - 如何使用 Selenium 2.0 和 Maven 运行 HTML Selenese 测试?

Python/ Selenium - "no such element: Unable to locate element"

java - 如何在没有任何唯一类属性的情况下单击按钮

Java - 使用 binarySearch 查找数组中特定元素的出现次数,无需 If

java - 读取字符串中每个 'if' 的每个单词

java - WebStart 可以使用 DownloadService 动态加载 jar 吗?

java - 'UnexpectedTagNameException' 和 Element 应该是 "select",但通过 Selenium java 使用 "div"函数却出现 'Select' 错误