我正在尝试使用带有Java的Selenium Webdriver自动发送Tweet。当我单击“ Tweet”时,将打开新的Tweet框,其中包含更改的URL。 element.sendKeys()到新的Tweet框,我得到ElementNotInteractableException:元素不可交互。使用js.executeScript(jsCommand,element)不会产生错误,但不会在“ Tweet”框中写入任何文本
我尝试使用action.moveToElement(),但“ Tweet”框不是模式窗口,它是同一窗口,具有相同的windowHandle,但URL不同,所以driver.switchTo()。window(handle)却没有”什么都不做。
代码打开Twitter(URL https://twitter.com/home),单击Tweet按钮,这将打开Tweet框(URL现在为https://twitter.com/compose/tweet),但是element.sendKeys(tweetMessage)给出了ElementNotInteractableException。
下面的代码段,//注释掉的行是我尝试过的:
// find the Tweet message box, enter the message and click Send
try {
windowURL = driver.getCurrentUrl();
System.out.println("Current window URL : " + windowURL);
System.out.println("Current window handle : " + driver.getWindowHandle());
// driver.navigate().to(windowURL); // makes no difference
// System.out.println("Switched to URL : " + windowURL);
// find the Tweet message box, doesn't matter if elementToBeClickable
// or visibilityOfElementLocated is used
// element = wait.until(
// ExpectedConditions.elementToBeClickable(By.xpath(composeTweetXpath)));
element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath(composeTweetXpath)));
System.out.println("After wait until...");
// driver.switchTo().frame("modal-header"); // not modal window
// System.out.println("After switchTo().frame()...");
action.moveToElement(element);
System.out.println("After moveToElement...");
// element.click(); // makes no difference, but gives the exception
// System.out.println("After element.click...");
element.sendKeys(tweetMessage); // this gives ElementNotInteractableException
System.out.println("After sendKeys : " + tweetMessage);
// jsCommand = "arguments[0].value='" + tweetMessage + "';";
// System.out.println("Javascript command : " + jsCommand);
// js.executeScript(jsCommand, element);
System.out.println("Found Tweet box and typed message");
driver.findElement(By.xpath(sendTweetXpath)).click();
System.out.println("Sent tweet : " + i.toString() + tweetMessage);
} catch ( Exception e1) {
System.out.println("Failed to send Tweet");
e1.printStackTrace();
}
输出: 当前窗口网址:https://twitter.com/compose/tweet
当前窗口句柄:CDwindow-878F9C73A9A7E11AEACDDECFF7A2CE85
等到...之后
moveToElement之后...
发送推文失败
org.openqa.selenium.ElementNotInteractableException:元素不可交互