首页 > 解决方案 > 无法通过 selenium 和 Java 从 Make My Trip 网站中的返回日期日历中找到任何日期

问题描述

在这里,我尝试选择 4 月 12 日的返回日期。我尝试使用不同的自定义 xpath 和 css 但无法找到元素:

    import java.util.List;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

public class Make_my_trip {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "F:\\Selenium\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.makemytrip.com/");

        // Selecting Return date
        driver.findElement(By.id("hp-widget__return")).click();
        //WebDriverWait ws = new WebDriverWait(driver, 10);
        //ws.until(ExpectedConditions.presenceOfElementLocated(By.id("dp1551508380301")));
        Thread.sleep(2000);;
        while (!driver.findElement(By.xpath("//span[@class ='ui-datepicker-month']")).getText().contains("April")) {

            System.out.println("Return date selected ");
            driver.findElement(By.xpath(
                    "div[@id='dp1551508898872']//span[@class='ui-icon ui-icon-circle-triangle-e'][contains(text(),'Next')"))
                    .click();
        }

        List<WebElement> returndate = driver.findElements(By.xpath("//a[@class ='ui-state-default']"));
        int count = returndate.size();

        for (int j = 0; j < count; j++) {
            String returndatetext = driver.findElements(By.xpath("//a[@class ='ui-state-default']")).get(i).getText();
            if (returndatetext.equalsIgnoreCase("12")) {
                driver.findElements(By.xpath("//a[@class ='ui-state-default']")).get(i).click();
                break;
            }
        }

    }

PS:

  1. 如果我们使用显式等待获取“ org.openqa.selenium.InvalidSelectorException ”错误,那么现在使用Thread.sleep(1000)
  2. 如果我们使用Xpath //div[@class='ui-datepicker-group ui-datepicker-group-last']/div/a/span[contains(text(),'Next' )][1]获取 org.openqa.selenium.ElementNotVisibleException: element not visible

标签: javaseleniumselenium-webdriver

解决方案


“元素不可见”。我也有这个错误。这意味着您的可搜索元素在当前快照中不可见。您应该专注于该元素或向下滚动直到元素出现在快照中


推荐阅读