测试场景:尝试捕获并测试 Gmail 登录。
电流输出:Mozilla 实例打开。输入了用户名但是
WebDriver 代码未输入密码。
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("[email protected] /cdn-cgi/l/email-protection");
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");
元素不可交互异常
根据文档,元素不可交互异常是 W3C 异常,抛出该异常表明虽然元素存在于DOM Tree https://javascript.info/dom-nodes,它不处于可以交互的状态。
原因及解决方案:
的原因元素不可交互异常发生的次数可能很多。
-
暂时覆盖其他网页元素超过网页元素我们感兴趣的:
在这种情况下,直接的解决方案就是诱导显式等待 i.e. WebDriver等待结合ExpectedCondition
as invisibilityOfElementLocated
如下:
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
as invisibilityOfElementLocated
我们可以用ExpectedCondition
as elementToBeClickable
如下:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
-
永久覆盖其他网页元素超过网页元素我们感兴趣的:
如果在这种情况下覆盖是永久性的,我们必须投射网络驱动程序实例为JavaScript执行器并执行如下点击操作:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
现在解决错误元素不可交互异常在这个特定的上下文中我们需要添加显式等待 i.e. WebDriver等待如下 :
你需要诱导一些wait为了Password字段在 HTML DOM 中正确呈现。您可以考虑配置一个显式等待为了它。以下是使用 Mozilla Firefox 登录 Gmail 的工作代码:
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("[email protected] /cdn-cgi/l/email-protection");
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)