【问题标题】:Getting error when trying to execute this code for MMT site尝试为 MMT 站点执行此代码时出错
【发布时间】:2020-10-18 16:34:40
【问题描述】:

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

【问题讨论】:

    标签: selenium selenium-webdriver nosuchelementexception


    【解决方案1】:

    您需要重新标识dropdown1,因为它第二次出现在源中的不同位置和不同的网络元素中。

    我看到了您的逻辑以及为什么您认为它具有相同的 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,但它是不同的 web 元素

        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 本机交互似乎很好。

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

    【讨论】:

    • 我重命名了。但是每当我尝试执行代码时,我都会为这一行得到 NoSuchElementException:
    • code WebElement fromCityItem = driver.findElement(By.id("react-autowhatever-1-section-0-item-0"));
    • 一旦它正常工作,但之后每次我尝试执行时,我都会在该行得到相同的错误
    • 它可能还需要在发送键和选择顶部项目之间的绘制延迟以让列表缓冲 - 或者您需要另一种方法来“选择顶部” - 在 mo 这只是一个盲点发送密钥完成后立即选择
    • 是的,我之前尝试过使用原生交互,但对于 codesource.click();声明,我得到了 ElementClickInterceptedException ,所以我尝试使用 Actions 进行交互
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 2019-01-26
    • 2021-12-24
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多