[英]Cannot sendKeys in a dropdown search bar 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);
}
上述方法將等到定位器可見,然后發送密鑰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.