首页 > 解决方案 > 一个的两个动态下拉代码工作正常,另一个不是。一次在 selenium java 中工作?

问题描述

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\Chrome Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        ///Code for source 
        Thread.sleep(5000);
        WebElement source=driver.findElement(By.id("fromCity"));
        source.sendKeys("mum");
        WebElement suggestion= driver.findElement(By.xpath("//input[@placeholder='From']"));
        suggestion.sendKeys(Keys.ARROW_DOWN);
        suggestion.sendKeys(Keys.ENTER);
        Thread.sleep(5000);
        
        ///Code for Destination

        WebElement destination = driver.findElement(By.id("toCity"));
        destination.sendKeys("Pak");
        WebElement suggestion2 = driver.findElement(By.xpath("//*[@placeholder='To']"));
        Thread.sleep(2000);
        suggestion2.sendKeys(Keys.ARROW_DOWN);
        suggestion2.sendKeys(Keys.ENTER);
        

标签: javaselenium

解决方案


这里的问题是,当您从下拉列表中输入“源”并单击 Enter 时,目标选项卡会自动打开。

在此处输入图像描述

现在,当您尝试点击以下代码时:

WebElement destination = driver.findElement(By.id("toCity"));
destination.sendKeys("Pak");

它不会运行,因为另一个元素在顶部

By.id("toCity"));IEBy.xpath("//*[@placeholder='To']")

在此处输入图像描述

我尝试单击它并收到以下错误:

Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input data-cy="toCity" id="toCity" type="text" class="fsw_inputField lineHeight36 latoBlack font30" readonly="" value="Bangalore"> is not clickable at point (480, 255). Other element would receive the click: 


<input type="text" autocomplete="off" aria-autocomplete="list" aria-controls="react-autowhatever-1" class="react-autosuggest__input react-autosuggest__input--open react-autosuggest__input--focused" placeholder="To" value="">
  (Session info: chrome=91.0.4472.106)

更正的代码:根据当前情况

///目的地代码

        WebElement suggestion2 = driver.findElement(By.xpath("//*[@placeholder='To']"));
        suggestion2.sendKeys("Pak");
        Thread.sleep(2000);
        suggestion2.sendKeys(Keys.ARROW_DOWN);
        suggestion2.sendKeys(Keys.ENTER);

推荐阅读