You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium无法选择应用中第二个自动填充字段值问题排查

问题排查与修复方案

Hey there, let's figure out why your destination field logic isn't working when running the full flow—even though it works alone. The core issue here is that your element locators and wait conditions aren't specific enough for the second field, leading to conflicts with the first field's leftover DOM elements. Here's the breakdown and fix:

What's Going Wrong?

  • Overly Broad XPath: Your //li[@class='ui-menu-item']//parent::li selects all matching <li> elements on the page. After selecting the source, those old suggestion elements might still be in the DOM, so your destination code ends up picking up the wrong list of options.
  • Unfocused Wait Conditions: You're waiting for all suggestion elements globally, not just the ones tied to the destination input. This means the code might start processing before the destination's suggestions actually load.
  • Using Keys.TAB Instead of Click: Tabbing away is an unreliable way to select an option—clicking the suggestion is the proper user-like action that ensures the input gets populated correctly.

Fixed Code

package com.qa.base;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
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 Sample {
    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Avanthi\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://www.abhibus.com/");

        // Handle Source Field
        WebElement sourceInput = driver.findElement(By.id("source"));
        sourceInput.sendKeys("Bang");
        WebDriverWait wait = new WebDriverWait(driver, 30);
        // Wait specifically for the source's suggestion box
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@aria-labelledby='ui-id-1']//li[@class='ui-menu-item']")));
        List<WebElement> sourceSuggestions = driver.findElements(By.xpath("//div[@aria-labelledby='ui-id-1']//li[@class='ui-menu-item']"));
        System.out.println("Source Suggestions Count: " + sourceSuggestions.size());
        for (WebElement option : sourceSuggestions) {
            System.out.println(option.getText());
            if (option.getText().equalsIgnoreCase("Bangalore Kempegowda Intl Airport")) {
                option.click();
                break;
            }
        }

        try {
            // Handle Destination Field
            WebElement destInput = driver.findElement(By.id("destination"));
            destInput.sendKeys("Chen");
            WebDriverWait destWait = new WebDriverWait(driver, 60);
            // Wait specifically for the destination's suggestion box (uses ui-id-2)
            destWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@aria-labelledby='ui-id-2']//li[@class='ui-menu-item']")));
            List<WebElement> destSuggestions = driver.findElements(By.xpath("//div[@aria-labelledby='ui-id-2']//li[@class='ui-menu-item']"));
            System.out.println("Destination Suggestions Count: " + destSuggestions.size());
            for (WebElement option : destSuggestions) {
                System.out.println(option.getText());
                if (option.getText().equalsIgnoreCase("Chengannur Railway Station")) {
                    option.click(); // Replace TAB with click for reliable selection
                    break;
                }
            }
        } catch (NoSuchElementException e) {
            e.printStackTrace();
            System.out.println("Element Not Found: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Unexpected Error: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

Key Changes Explained

  • Targeted Locators: We use the aria-labelledby attribute to distinguish between the source (ui-id-1) and destination (ui-id-2) suggestion boxes. This ensures we only pull options for the active input field.
  • Input-Specific Waits: Instead of waiting for all suggestion elements, we wait directly for the destination's suggestion box to load—no more racing against leftover elements from the source field.
  • Replace TAB with Click: Clicking the suggestion mimics real user behavior, guaranteeing the input gets the correct value and avoids focus issues from tabbing.
  • Reusable Element Variables: Storing input elements as variables makes the code cleaner and reduces redundant DOM lookups.

If you still run into issues, try disabling implicit wait (since mixing implicit and explicit waits can cause timing conflicts) or add a short wait after selecting the source to let the page stabilize. Also, check for any popups or overlays that might be blocking the destination input.

内容的提问来源于stack exchange,提问作者Testing Passion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:12:14