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



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

搜索栏图片(单击"人物"时(

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);
}

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

最新更新