尝试为MMT站点执行此代码时出现错误

问题描述

我使用了以下代码。当我尝试运行此代码以创建旅行站点时,出现NoSuchElementException错误

public class suggestiveDropdown_MMTsite {
  public static void main(String[] args) throws InterruptedException {
    // Todo Auto-generated method stub
    System.setProperty("webdriver.chrome.driver","C:\\Webdriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.makemytrip.com/");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

    Actions a = new Actions(driver);

    WebElement source = driver.findElement(By.id("fromCity"));
    a.movetoElement(source).click().build().perform();
    a.movetoElement(source).sendKeys("MUM").build().perform();
    /*webdriverwait w = new webdriverwait(driver,5);
    w.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("react-autowhatever-1")));*/

    WebElement dropdown1 = driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
    a.movetoElement(dropdown1).click().build().perform();
    WebElement destination = driver.findElement(By.id("toCity"));
    a.movetoElement(destination).click().build().perform();
    a.movetoElement(destination).sendKeys("DEL").build().perform();
    a.movetoElement(dropdown1).click().build().perform();
  }
}

解决方法

您需要重新标识dropdown1,因为它第二次出现在源中的其他位置,并且是另一个Web元素。

我了解了您的逻辑以及为什么您认为它具有相同的ID会起作用-但是,当您使用By时,您是从DOM返回特定的网络元素。即使由于任何JavaScript魔术而重复使用ID, webelement 还是有所不同(毕竟位置不同)。

我正在谈论的两位。...

1 /您的代码的这一部分找到对象:

    WebElement dropdown1 = driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
    a.moveToElement(dropdown1).click().build().perform();

2 /不再是下拉列表1。它具有相同的ID,但它是一个不同的网络元素

    a.moveToElement(dropdown1).click().build().perform();

解决方案是重新获取该元素,我建议重命名该元素以帮助阐明区别。将最后一行更改为以下内容:

    WebElement firstItemInToCity= driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
    a.moveToElement(firstItemInToCity).click().build().perform();

我又看了。这对我有用:

        driver.get("https://www.makemytrip.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

        //Actions a = new Actions(driver);

        //from city:
        WebElement source = driver.findElement(By.id("fromCity"));
        source.click();
        source.sendKeys("MUM");

        //select first item in the list
        WebElement dropdown1 = driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
        dropdown1.click();

        //Get the to city
        WebElement destination = driver.findElement(By.id("toCity"));
        destination.click();
        destination.sendKeys("DEL");
        
        //pick the first item in the list
        WebElement firstItemInToCity= driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
        firstItemInToCity.click();

您不需要使用操作进行交互。使用硒本机相互作用似乎很好。

我确实增加了隐式超时,因为页面报价对我来说加载很慢。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...