【问题标题】:PhantomJS is not able to find the element with selenium webdriverPhantomJS 无法使用 selenium webdriver 找到元素
【发布时间】:2013-09-10 18:36:28
【问题描述】:

我有一个 Selenium 网络驱动程序脚本,它只访问一个网络元素(文本框)并将其值设置为某个值。

我正在使用 ChutzPath,因为它在后端使用 PhantomJS 浏览器。

但是每次执行脚本,都会显示如下错误。

T
Result Message: 
Test method Test_Project_with_Selenium.UnitTest1.BrowseTheWeb threw exception: 

OpenQA.Selenium.NoSuchElementException: Error Message => 'Unable to find element with id 'inputEl''
 ***caused by Request => {"headers":{"Accept":"application/json, image/png","Connection":"Close","Content-Length":"47","Content-Type":"application/json;charset=utf-8","Host":"localhost:42982"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"id\",\"value\":\"textfield-1042-inputEl\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/01225660-1a45-11e3-8ea4-eff85782779d/element"}
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
   at Test_Project_with_Selenium.UnitTest1.BrowseTheWebClient() in c:\Users\XYZ\Documents\Visual Studio 2012\Projects\ProjectName\Test_Project_with_Selenium\UnitTest1.cs:line 19***

【问题讨论】:

  • 查看 HTML 会很有帮助。袖手旁观,您可能会遇到在 Selenium 查询页面后元素加载的问题(并且您可以使用隐式等待您想要出现的元素 - driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds( 10));.) 或者您要查找的 id 在框架/iframe 内,在这种情况下,您需要使用 Driver.SwitchTo().Frame("frameIdentifier");
  • @Richard,继续把它放在答案中。你很准。
  • 展示脚本的使用(相关部分)也很重要,这样我们才能真正看到您的代码并查找错误。

标签: selenium selenium-webdriver phantomjs chutzpah


【解决方案1】:

不确定这里是否是这种情况,因为问题不是很具体,但我遇到的一件事是 PhantomJS 1.9.7 需要该位置的 HTTP 方案。

因此,如果您正在导航 localhost:42982/element,则应指定要导航到 http://localhost:42982/element 的驱动程序。

【讨论】:

【解决方案2】:

即兴发挥,您可能遇到了元素在 Selenium 查询页面后加载的问题,您可以使用 waitfor.js

https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js

或者可能是这样的:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)

或者您要查找的 id 在框架/iframe 内,在这种情况下您需要使用

Driver.SwitchTo().Frame("frameIdentifier");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2016-08-31
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多