无法在下拉搜索栏中发送键 Java Selenium Webdriver

Cannot sendKeys in a dropdown search bar Java Selenium Webdriver

我正在尝试将 London 输入到以下下拉搜索栏中,但它没有发送密钥。努力了解如何正确抓取元素?我至少可以让搜索加载..

Search Bar picture (when clicking People)

    public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "/Users/Desktop/chromedriver" );

    WebDriver driver = new ChromeDriver();

    driver.get("https://www.dlapiper.com/en/uk/");

    WebElement peopleButton = driver.findElement(By.id("ui-id-1"));
    peopleButton.click();

    WebElement peopleAutoComplete = driver.findElement(By.id("peopleglobalsearchbox"));

    peopleAutoComplete.sendKeys("London");

这将因两个原因而失败。首先,您找到了错误的元素。其次,您将在它出现在页面上之前识别它。

使用 Selenium sendKeys 时,您应该引用输入字段,而不是 div。在这种情况下:

By.xpath("//div[@id='peopleglobalsearchbox']//input");

元素的可见性更为复杂,但 Selenium 在支持这些挑战方面做得很好。

    public WebElement waitUntilElementIsVisible(By by) {
        new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(by));
        return driver.findElement(by);
    }

上述方法将等到定位器可见,然后发送密钥。