Selenium - IE 8 - 无法找到带链接文本的元素

时间:2014-03-19 15:26:09

标签: selenium-webdriver internet-explorer-8 screenshotexception

在Selenium测试中(使用版本2.40.0),我无法远程使用Internet Explorer 8找到包含链接文本的元素。它适用于Chrome。

这里有一个带有公共网站的(groovy)样本。

import org.junit.Test
import org.openqa.selenium.By
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.remote.RemoteWebDriver

class SampleIT {

    @Test
    void test() {
        def abilities = DesiredCapabilities.internetExplorer()
        def webDriver = new RemoteWebDriver(new URL("http://10.50.26.116:4444/wd/hub"), abilities)
        webDriver.get("http://www.seznam.cz")
        webDriver.findElement(By.linkText("Firmy")).click()
    }

}

这是日志

   org.openqa.selenium.NoSuchElementException: Unable to find element with link text == Firmy (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 285 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28'
System info: host: 'ghana-selenium', ip: '10.50.26.116', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 78d4e326-4916-4aee-9834-dafbc304a8ed
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=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:44587/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
Command duration or timeout: 424 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: '4c5c0568b004f67810ee41c459549aa4b09c651e', time: '2014-02-19 11:13:01'
System info: host: 'ubuntu', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.11.0-18-generic', java.version: '1.7.0_40'
Session ID: c542b45e-03d0-43dd-ba5f-c8748d732d85
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, webdriver.remote.sessionid=c542b45e-03d0-43dd-ba5f-c8748d732d85, version=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:44587/, 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:573)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:326)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:375)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:242)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:318)
    at org.openqa.selenium.WebDriver$findElement$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at SampleIT.test(SampleIT.groovy:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.40.0', revision: '4c5c0568b004f67810ee41c459549aa4b09c651e', time: '2014-02-19 11:13:01'
System info: host: 'ubuntu', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.11.0-18-generic', java.version: '1.7.0_40'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:125)
    ... 36 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with link text == Firmy (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 285 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28'
System info: host: 'ghana-selenium', ip: '10.50.26.116', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 78d4e326-4916-4aee-9834-dafbc304a8ed
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=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:44587/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: '4c5c0568b004f67810ee41c459549aa4b09c651e', time: '2014-02-19 11:13:01'
System info: host: 'ubuntu', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.11.0-18-generic', java.version: '1.7.0_40'
Driver info: driver.version: EventFiringWebDriver
    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:573)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:326)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:375)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:242)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:318)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:101)
    at com.sun.proxy.$Proxy2.findElement(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:184)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:47)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:170)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)


Process finished with exit code 255

修改 我在家里尝试过它并且有效。但在办公室,问题持续存在。 IE可能会受到网络设置,Windows域等的影响吗?

2 个答案:

答案 0 :(得分:3)

我认为你的编辑可能在正确的轨道上。

Selenium Internet Explorer wiki,您可以看到以下必需的配置:

  
      
  • 在Windows Vista或Windows 7上的IE 7或更高版本中,必须将每个区域的保护模式设置设置为相同的值。只要每个区域的值相同,该值就可以打开或关闭。要设置保护模式设置,请从“工具”菜单中选择“Internet选项...”,然后单击“安全”选项卡。对于每个区域,标签底部将显示一个标记为“启用保护模式”的复选框。   此外,必须为IE 10及更高版本禁用“增强保护模式”。此选项位于“Internet选项”对话框的“高级”选项卡中。

  •   
  • 浏览器缩放级别必须设置为100%,以便可以将本机鼠标事件设置为正确的坐标。

  •   
  • 仅对于IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以维护与其创建的Internet Explorer实例的连接。对于32位Windows安装,您必须在注册表编辑器中检查的密钥是HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Internet Explorer \ Main \ FeatureControl \ FEATURE_BFCACHE。对于64位Windows安装,密钥为HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Internet Explorer \ Main \ FeatureControl \ FEATURE_BFCACHE。请注意,FEATURE_BFCACHE子项可能存在也可能不存在,如果不存在,则应创建该子项。重要说明:在此项中,创建名为iexplore.exe的值为0的DWORD值。

  •   

我的猜测是你的问题是保护模式。

答案 1 :(得分:1)

Selenium只等待了285毫秒才能在页面上看到链接。您可以尝试使用implicit or explicit等待,等待的时间足够长,以确保在加载页面后元素是否存在。< / p>

并确保Firmy是您的锚标记中存在的确切字词,即使有一个额外的空格,它也会引发Nosuchelement例外。如果不确定您可以使用PartialLinkText

这是使用显式等待:

new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Firmy"))).click();

这将等待30秒,以便元素可见。