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

时间:2020-10-18 16:34:40

标签: selenium selenium-webdriver nosuchelementexception

我使用了以下代码。当我尝试运行此代码以创建旅行站点时,出现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();
  }
}

1 个答案:

答案 0 :(得分:0)

您需要重新标识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();

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

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

相关问题