作为学习 Selenium WebDriver 的练习,我正在尝试访问 Twitter log in page并登录。我终于能够导航到该页面并获取用户名字段所需的 * 元素:
<input class="text-input email-input js-signin-email" name="session[username_or_email]" type="text">
*我试图找到什么类型的元素sendKeys
可以操作,但我不能。我认为这个元素是我想要的,因为标签是 <input>
类型。
当我打电话sendKeys
时在分配元素后,我得到一个 ElementNotInteractableException
。我尝试过使用显式等待 ExpectedConditions
的visibilityOf
和invisibilityOf
。 TimeoutException
的各自解读和ElementNotInteractableException
告诉我上面提到的元素会加载到 DOM 树中,但永远不会变得可见*。我该如何解决这个问题?有没有使用 JavascriptExecutor
的解决方案?
*周围的 html 可能会揭示该元素保持不可见的原因:
<form class="t1-form clearfix signin js-signin"
action="https://twitter.com/sessions" method="post">
::before
<fieldset>
<legend class="visuallyhidden">Log in</legend>
<div class="clearfix field">
::before
<input class="text-input email-input js-signin-email"
name="session[username_or_email]" type="text">
.
**对于那些正在寻找的人,this问题很有帮助,特别是如果问题是由 click
引起的方法代替。
最佳答案
您可以使用此代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) throws InterruptedException {
final WebDriver driver = new ChromeDriver();
driver.get("https://twitter.com/login?lang=en");
WebDriverWait wait = new WebDriverWait(driver, 10);
// locate login input
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@placeholder = 'Phone, email or username']")));
WebElement loginInput = driver.findElement(By.xpath("//input[@placeholder = 'Phone, email or username']"));
loginInput.click();
loginInput.sendKeys("login");
// locate password input
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//fieldset//input[@placeholder = 'Password']")));
WebElement passInput = driver.findElement(By.xpath("//fieldset//input[@placeholder = 'Password']")); // this xPath does the trick
passInput.click();
passInput.sendKeys("password");
Thread.sleep(3000); // only to see the result
//locate submit button
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@type= 'submit']")));
WebElement submitBtn = driver.findElement(By.xpath("//button[@type= 'submit']"));
submitBtn.click();
Thread.sleep(3000); // only to see the result
driver.quit();
}
}
关于java - 如何解决Selenium WebDriver的sendKeys方法抛出的ElementNotInteractableException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51070700/