selenium 发送 xpath 的密钥不起作用

selenium send key for xpath not working

我想为此实现自动化 web site 在这个网站上有 3 个文本框在这里检查图片

第一个文本框 x 路径是 /html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]

这是我的代码

driver.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]")).sendKeys("rio salon");

当我 运行 这个代码时我得到了这个错误

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard

我该如何解决?我希望我的 xpath 是正确的。

该字段具有 aria-hidden=true 属性,因此您得到 ElementNotInteractableException。您需要先点击下拉菜单将属性更改为 true

WebElement dropdown = driver.findElement(By.xpath("//*[@id='search-form']/div/div[1]/div/div[1]/span"));
dropdown.click();
WebElement textField = dropdown.findElement(By.xpath("./parent::div/following-sibling::input[contains(@class, 'ui-select-search')]"));
textField.sendKeys("rio salon");

您可以单击带有 divspan 标记的输入字段,但不能在该字段中键入内容。因此,如果您想要 sendkeys 或在输入字段中键入,则必须使用 input 标记编写您的 XPath。例如:

//input[contains(@placeholder,'Site')]