在手动打开链接时,无法通过selenium打开链接

时间:2014-04-04 14:19:11

标签: security url selenium-webdriver

我需要在某个网站上自动填写一个表单的过程。我可以轻松地手动完成。我去网站,登录并点击链接 - 转到表格填写。

然后我运行selenium测试,它失败然后点击链接转到表单填写。错误是:

Internet Explorer不会显示网页。

我想这可能是因为本网站的一些安全规则。但我不知道如何解决它。 有人可以告诉我在这种场合可以做些什么吗?感谢

更新: 2)这个网站AJAX很重吗? - 不 3)您使用隐式或显式计时器吗? - 我试过明确的等待 - 没有帮助 4)什么代码失败以及IDE抛出了什么错误消息 - 错误是驱动程序无法找到元素。在打开之后,这个元素变得可见。由于链接无法打开 - 引发此错误。所以我从IE写错误信息,因为它是根本原因。 5)HtmlUnitDriver - 不要尝试

代码:

            driver.get(linkToConsole);
    driver.findElement(By.xpath(loginLocator)).sendKeys(login);
    driver.findElement(By.xpath(passwordLocator)).sendKeys(password);
    driver.findElement(By.xpath(submitButton)).click();
    wait = new WebDriverWait(driver, 5000);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(goToHomePageLocator)));
    //this link doesn't open (but it works manually)
            driver.findElement(By.xpath(goToHomePageLocator)).click();
    // this element is not found and here error in IDE is raised
    driver.findElement(By.id(manualValidation)).click();

错误:

org.openqa.selenium.NoSuchElementException: 
Unable to find element with id == myid (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info:
Session ID: 0976b47d-caf5-42fc-9e48-5c8a8c18e624
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=true, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://localhost:12015/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
at org.openqa.selenium.By$ById.findElement(By.java:220)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at test.java.org.fullProcess.fillConsole(FullProcess.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:0)

尝试更改保护模式设置。

工具 - >互联网选项 - >安全选项卡 - >为所有区域选择“启用保护模式”。

我认为这应该是你问题的原因。

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
ieCapabilities.setCapability("ensureCleanSession", true);
driver = new InternetExplorerDriver(ieCapabilities);

来源:http://mestachs.wordpress.com/2012/08/13/selenium-best-practices/