Selenium WebDriver在线程“main”中抛出Exception。 org.openqa.selenium.ElementNotInteractableException [英] Selenium WebDriver throws Exception in thread "main" org.openqa.selenium.ElementNotInteractableException

查看:1346
本文介绍了Selenium WebDriver在线程“main”中抛出Exception。 org.openqa.selenium.ElementNotInteractableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试场景:尝试捕获并测试Gmail登录。

Test Scenario: Trying to capture and test Gmail Login.

当前输出: Mozilla实例打开。输入用户名但
WebDriver代码未输入密码。

Current Output: Mozilla instance opens up. Username is entered but the Password is not being entered by the WebDriver code.

System.setProperty("webdriver.gecko.driver", "C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
FirefoxDriver  varDriver=new FirefoxDriver();

varDriver.get("http://gmail.com");  
WebElement webElem=  varDriver.findElement(By.id("identifierId"));
webElem.sendKeys("error59878@gmail.com");
WebElement nextButton=varDriver.findElement(By.id("identifierNext"));
nextButton.click();

varDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

WebElement wePass=varDriver.findElement(By.cssSelector(".rFrNMe.P7gl3b.sdJrJc.Tyc9J"));

wePass.sendKeys("test1");


推荐答案

什么是 ElementNotInteractableException



根据文档, ElementNotInteractableException 是W3C异常,抛出此异常表示虽然 DOM TREE 中有一个元素,但它不在某个状态可以与之互动。

What is ElementNotInteractableException?

As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM TREE, it is not in a state that can be interacted with.

发生 ElementNotInteractableException 的原因可能很多。

The reason for ElementNotInteractableException to occur can be numerous.


  1. 上的其他 WebElement 的临时叠加层>我们感兴趣的Web元素

  1. Temporary Overlay of other WebElement over the WebElement of our interest :

在这种情况下,直接解决方案是诱导 ExplicitWait WebDriverWait ExpectedCondition相结合 as invisibilityOfElementLocated as folllows:

In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as folllows:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();

更好的解决方案是更细化,而不是使用 ExpectedCondition invisibilityOfElementLocated 我们可以使用 ExpectedCondition elementToBeClickable ,如下所示:

A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();


  • 永久叠加其他 WebElement 超过我们感兴趣的 WebElement

  • Permanent Overlay of other WebElement over the WebElement of our interest :

    如果叠加层是永久叠加层在这种情况下,我们必须将 WebDriver 实例强制转换为 JavascriptExecutor 并按如下方式执行单击操作:

    If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);
    







  • 现在在此特定上下文中解决错误 ElementNotInteractableException ,我们需要添加 ExplicitWait WebDriverWait ,如下所示:


    Now addressing the error ElementNotInteractableException in this particular context we need to add ExplicitWait i.e. WebDriverWait as follows :

    您需要诱导一点等待,以便在HTML DOM中正确呈现密码字段。您可以考虑为它配置 ExplicitWait 。以下是使用Mozilla Firefox登录Gmail的工作代码:

    You need to induce a bit of wait for the Password field to be properly render in the HTML DOM. You can consider configuring an ExplicitWait for it. Here is the working code to login into Gmail using Mozilla Firefox:

    System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    String url = "https://accounts.google.com/signin";
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
    email_phone.sendKeys("error59878@gmail.com");
    driver.findElement(By.id("identifierNext")).click();
    WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(password));
    password.sendKeys("test1");
    driver.findElement(By.id("passwordNext")).click();
    

    这篇关于Selenium WebDriver在线程“main”中抛出Exception。 org.openqa.selenium.ElementNotInteractableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆