硒网络自动化中元素不可交互异常

rag*_*993 7 java testng selenium automation webautomation

在下面的代码中,我无法在密码字段中发送密码密钥,我尝试单击该字段,清除该字段并发送密钥。但是现在可以使用任何一种方法。但是如果我调试和测试它的工作

  public class TestMail {
   protected static WebDriver driver;

   protected static String result;

   @BeforeClass

   public static void setup()  {
              System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe");

   driver = new FirefoxDriver();

   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

  }

   @Test

 void Testcase1() {

   driver.get("http://mail.google.com");

   WebElement loginfield = driver.findElement(By.name("Email"));
   if(loginfield.isDisplayed()){
       loginfield.sendKeys("ragesh@gmail.in");
   }
   else{
  WebElement newloginfield = driver.findElemnt(By.cssSelector("#identifierId"));                                      
       newloginfield.sendKeys("ragesh@gmail.in");
      // System.out.println("This is new login");
   }


    driver.findElement(By.name("signIn")).click();

  // driver.findElement(By.cssSelector(".RveJvd")).click();

   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 // WebElement pwd = driver.findElement(By.name("Passwd"));
  WebElement pwd = driver.findElement(By.cssSelector("#Passwd"));

  pwd.click();
  pwd.clear();
 // pwd.sendKeys("123");
 if(pwd.isEnabled()){
     pwd.sendKeys("123");
 }
 else{
     System.out.println("Not Enabled");
 }
Run Code Online (Sandbox Code Playgroud)

use*_*882 26

“元素不可交互”错误可能意味着两件事:

A。 元素未正确呈现:

解决方案就是使用隐式/显式等待

  • 隐式等待:

    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

  • 显式等待:

    WebDriverWait 等待=新的 WebDriverWait(驱动程序, 20); element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className("fa-stack-1x")));

b. 元素已渲染,但不在屏幕的可见部分:

解决方案就是滚动直到该元素。根据 Selenium 的版本,可以用不同的方式处理,但我将提供一个适用于所有版本的解决方案:

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].scrollIntoView(true);", element1);
Run Code Online (Sandbox Code Playgroud)
  1. 假设这一切都失败了,那么另一种方法是再次使用 Javascript 执行器,如下所示:

    executor.executeScript("arguments[0].click();", element1);

  2. 如果您仍然无法点击,那么这可能又意味着两件事:

1. 内嵌框架

检查 DOM 以查看您正在检查的元素是否存在于任何框架中。如果这是真的,那么您需要在尝试任何操作之前切换到此框架。

    driver.switchTo().frame("a077aa5e"); //switching the frame by ID
    System.out.println("********We are switching to the iframe*******");
    driver.findElement(By.xpath("html/body/a/img")).click();
Run Code Online (Sandbox Code Playgroud)

2. 新标签页

如果打开了一个新选项卡并且该元素存在于其中,那么您再次需要编写如下代码以在尝试操作之前切换到它。

String parent = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Continue")).click();
Set<String> s = driver.getWindowHandles();
// Now iterate using Iterator
Iterator<String> I1 = s.iterator();
while (I1.hasNext()) {
String child_window = I1.next();
if (!parent.equals(child_window)) {
    driver.switchTo().window(child_window);
    element1.click() 
}
Run Code Online (Sandbox Code Playgroud)


ans*_*pta 10

尝试将隐式等待时间设置为大约10秒。

gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

或设置一个明确的等待。显式等待是您定义的代码,用于等待特定条件发生后再继续执行代码。您的情况就是密码输入字段的可见性。(感谢ainlolcat的评论)

WebDriver gmail= new ChromeDriver();
gmail.get("https://www.gmail.co.in"); 
gmail.findElement(By.id("Email")).sendKeys("abcd");
gmail.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");
Run Code Online (Sandbox Code Playgroud)

说明:硒无法找到该元素的原因是因为密码输入字段的ID最初是隐藏的密码。单击“下一步”按钮后,Google首先验证输入的电子邮件地址,然后显示密码输入字段(将ID从“隐藏密码”更改为“密码”)。因此,当密码字段仍处于隐藏状态(即Google仍在验证电子邮件ID)时,您的网络驱动程序将开始搜索ID为Passwd且仍处于隐藏状态的密码输入字段。因此,将引发异常。


Ros*_*ech 5

请尝试像这样选择密码字段。

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
    passwordElement.click();
  passwordElement.clear();
     passwordElement.sendKeys("123");
Run Code Online (Sandbox Code Playgroud)


小智 5

您也可以尝试完整的 xpath,我遇到了类似的问题,我必须单击具有属性 javascript onclick 函数的元素。完整的 xpath 方法有效并且没有抛出可交互的异常。