尝试使用硒在文本区域中书写
Trying to write in text area using selenium
我正在尝试使用 selenium (java) 在 Instagram post 中发表评论。 textArea
中的 Instagram 评论框,如何添加评论?
我这样做了...
WebElement cmdbox = driver.findElement(By.tagName("textarea"));
cmdbox.clear();
Thread.sleep(2000);
cmdbox.sendKeys("sample text");
Thread.sleep(3000);
我遇到了这个错误
抛出 ElementNotInteractableException 表示尽管元素存在于 DOM 上,但它不处于可以与之交互的状态。
又名它在 DOM 中,但您还不能点击它。有几种方法可以解决这个问题,使用 ExpectedConditions.elementToBeClickable()
是一种方法。在调用 click 之前,辅助方法可能会有所帮助。
public static void waitForElementClickable(WebDriver webdriver, By by, long timeout) {
WebDriverWait wait = new WebDriverWait(webdriver, timeout);
wait.until(ExpectedConditions.elementToBeClickable(by));
}
WebDriverWait wait = new WebDriverWait(webdriver, timeout); //15 can be ideal for timeout
//清除区域的代码
wait.until(ExpectedConditions.elementToBeClickable(By.tagName("textarea"))).clear();
//发送密钥的代码
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("textarea"))).sendKeys("sample text");
如果这不起作用,您可以使用 By.CssSelector("textarea.Ypffh") 作为 [= 的另一种替代方法25=]("文本区域")
我正在尝试使用 selenium (java) 在 Instagram post 中发表评论。 textArea
中的 Instagram 评论框,如何添加评论?
我这样做了...
WebElement cmdbox = driver.findElement(By.tagName("textarea"));
cmdbox.clear();
Thread.sleep(2000);
cmdbox.sendKeys("sample text");
Thread.sleep(3000);
我遇到了这个错误
ElementNotInteractableException 表示尽管元素存在于 DOM 上,但它不处于可以与之交互的状态。
又名它在 DOM 中,但您还不能点击它。有几种方法可以解决这个问题,使用 ExpectedConditions.elementToBeClickable()
是一种方法。在调用 click 之前,辅助方法可能会有所帮助。
public static void waitForElementClickable(WebDriver webdriver, By by, long timeout) {
WebDriverWait wait = new WebDriverWait(webdriver, timeout);
wait.until(ExpectedConditions.elementToBeClickable(by));
}
WebDriverWait wait = new WebDriverWait(webdriver, timeout); //15 can be ideal for timeout
//清除区域的代码
wait.until(ExpectedConditions.elementToBeClickable(By.tagName("textarea"))).clear();
//发送密钥的代码
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("textarea"))).sendKeys("sample text");
如果这不起作用,您可以使用 By.CssSelector("textarea.Ypffh") 作为 [= 的另一种替代方法25=]("文本区域")