2016-11-04 47 views
-1

以下代码在IE,FireFox,Chrome中执行完美。在PhantomJS中,它似乎并不等待这种情况,因为弹出窗口(通过CSS上的CSS样式)显示。如果我在发生异常的语句之前放置睡眠语句,问题就会消失。为什么是driver.ImplicitlyWait()不工作?我应该不必乱丢我的代码,并应该与幻影等待?InvalidElementStateException使用PhantomJS 2.1.1/Webdriver 2.53.0和Visual Studio/C#

我看了很多帖子,似乎没有人能解决我的问题。任何想法,我可以尝试什么?

事实和我试过的东西...
- 在整个源代码中只有一个id ='firstname'的元素。
- 无论浏览器如何,始终可以找到此元素,这是一个可交互的问题。
- 此元素始终可以在ff,chrome中交互,即没有WebDriverWait。
- 除非使用WebDriverWait,否则此元素永远不会以幻像交互。
- 设置driver.Manage()。Window.size为Maximum或1920,1080不能解决问题。 - 尝试不同的选择器,如By.CssSelector(“input#firstname”)和By.XPath(“// div [@ class ='modal-body'] // input [@ id ='firstname']”)导致同样的例外。

// successful code 
IWebDriver driver = new PhantomJSDriver(); 
driver.Manage().Window.Size = new System.Drawing.Size(1920, 1080); 
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); 
driver.Navigate().GoToUrl(home_page); 
driver.FindElement(By.Id("btnAddEmployee")).Click(); 

// exception here 
driver.FindElement(By.Id("firstname")).SendKeys("first name"); 

//例外 OpenQA.Selenium.InvalidElementStateException:{ “的errorMessage”: “元素目前交互的和可能不被操纵”, “请求”:{ “报头”:{ “接受”:” “application/json,image/png”,“Connection”:“Close”,“Content-Length”本地主机:20943 “},” httpVersion “:” 1.1" , “方法”: “POST”, “后”: “{\” 值\ “:[\” 爱丽丝\ “]}”, “URL”:“/值”, “urlParsed”:{ “锚”: “”, “查询”: “”, “文件”: “值”, “目录”: “/”, “路径”: “/值”, “相对的” : “/值”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “”, “用户信息”: “”, “权威”: “”,“协议“:” “ ”源“: ”/值“, ”queryKey“:{}, ”块“:[ ”值“]} ”urlOriginal“:”/会话/ 5c641300-a229-11e6-9fa8-69dbd56d6ff7 /元素/:wdc:1478220923048 /值“}} 结果StandardOutput:
无法连接到远程服务器 OpenQA.Selenium.InvalidElementStateException:{“errorMessage”:“元素当前不可交互,可能无法操作”,“request”:{“headers”:{“Accept “:”application/json,image/png“,”Connection“:”Close“,”Content-Length“:”19“,”Content-Type“:”application/json; charset = utf-8“ “:” 本地主机:20943 “},” httpVersion “:” 1.1" , “方法”: “POST”, “后”: “{\” 值\ “:[\” 爱丽丝\ “]}”, “URL” : “/值”, “urlParsed”:{ “锚”: “”, “查询”: “”, “文件”: “值”, “目录”: “/”, “路径”: “/值”, “相对”: “/值”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “”, “用户信息”: “”, “权威”: “” , “协议”: “”, “源”: “/值”, “queryKey”:{}, “块”:[ “值”]} “urlOriginal”:“/会话/ 5c641300-a229-11e6-9fa8 -69dbd56d6ff7/element /:wdc:1478220923048/value“}} at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(响应错误响应) 在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串driverCommandToExecute,Dictionary`2参数) 在OpenQA.Selenium.Remote.RemoteWebElement.SendKeys(字符串文本) 在Test.TestCases.Test()在C: \用户\用户\文档\ Visual Studio的2015年\项目\测试\项目\ TestCases.cs:行191

//例外截图 exception screenshot

// web page source code being automated 
<!DOCTYPE html><html lang="en"><head> 
    <title>Benefits Dashboard</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="assets/css/pagePuncher.css"> 
    <link href="https://fonts.googleapis.com/css?family=Merriweather+Sans:300" rel="stylesheet" type="text/css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">  
    </script> 
    <script src="http://fgnass.github.io/spin.js/spin.min.js"></script><style type="text/css"></style> 
    <script src="assets/js/employee.js"></script> 
    <script src="assets/js/scripts.js"></script> 
    <script> 
    function GetURLParameter(r){for(var t=window.location.search.substring(1),e=t.split("&"),n=0;n<e.length;n++){var a=e[n].split("=");if(a[0]==r)return a[1]}return""}""==GetURLParameter("username")&&(window.location.href="login.html"); 
    </script> 
</head> 
<body class="modal-open"> 
    <div id="uselessBlueHeader"></div> 
    <div class="container"> 
    <div id="header" class="page-header jumbotron"> 
    <h1>Benefits Dashboard</h1> 
    </div> 
    <div id="data"> 
    <div class="table-responsive"> 
    <table class="table table-striped table-hover" id="employee-table" style="margin-bottom: 5px;"> 
     <thead> 
      <tr> 
       <th>ID</th> 
       <th>Last Name</th> 
       <th>First Name</th> 
       <th>Salary</th> 
       <th>Dependents</th> 
       <th>Gross Pay 
       </th><th>Benefit Cost</th> 
       <th>Net Pay</th> 
       <th>Actions</th> 
      </tr> 
     </thead> 
     <tbody></tbody> 
    </table> 
    </div> 
    </div> 
    <button id="btnAddEmployee" type="button" class="btn btn-primary" data-toggle="modal" data-target="#addEmployeeModal">Add Employee</button> 

    <div class="modal fade in" id="addEmployeeModal" role="dialog" style="display: block;"> 
    <div class="modal-dialog modal-lg"> 

     <!-- Modal content--> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal">×</button> 
      <h4 class="modal-title">Add Employee &amp; His dependents</h4> 
     </div> 
     <div class="modal-body"> 

      <form class="form-horizontal" role="form" id="employees-form" onsubmit="return validateForm()"> 

      <div class="form-group"> 
       <label class="control-label col-sm-2" for="firstname">First Name:</label> 
       <div class="col-sm-10"> 
        <input type="text" class="form-control" id="firstname"> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="control-label col-sm-2" for="lastname">Last Name:</label> 
       <div class="col-sm-10"> 
        <input type="text" class="form-control" id="lastname"> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="control-label col-sm-2" for="dependents">Dependants:</label> 
       <div class="col-xs-1"> 
        <input type="text" class="form-control" id="dependents"> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-sm-offset-2 col-sm-10"> 
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="addEmployee">Submit</button> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
      </form> 
     </div> 
     </div> 
    </div> 
    </div> 
    </div> 

<div class="modal-backdrop fade in"></div></body></html> 
+0

你可以尝试使用不同的选择器来查找元素。 – Abhinav

+0

另外,你可以尝试使用最大化浏览器:driver.manage()。window()。maximize();我看到几个帖子称这是解决方法。请让知道,如果有帮助 – Abhinav

+0

感谢您的建议Abhinav。我已经更新了“我试过的事实和事情”的帖子。很遗憾,您的建议无法解决问题。 – user1701153

回答

0

使用隐式等待等待,直到元素可见在dom中,并且不检查其他交互(例如元素状态)。一世。E,在这行代码

driver.FindElement(By.Id("firstname")).click() 

隐含的等待只等着与id="firstname"元素出现,一旦这样做,该脚本将尝试一下就可以了。但是,元素可能不是“可点击”的。行为可能因多种因素而异,这可能是您浏览器遇到不稳定执行的原因。

要达到此目的,您必须特意等待,直到元素可点击。您可以使用这样的事情:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); 
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("firstname"))); 

注意 1.不要在你的代码混合隐性和显性等待声明。 2.如果你想拥有稳定的脚本,正确的同步是一个重要的因素。同步不仅仅是添加等待语句。 3.使用睡眠语句抛弃代码并不是很好的编程习惯,理想情况下不应该使用,除非您只是试图调试脚本。

其他细节 我怀疑其他浏览器和phantomjs之间的区别在于底层浏览器引擎。例如,Phantomjs使用WebKit,Chrome使用Blink

您不必在每个操作之前使用显式等待。紧跟在导致另外的Dom元素加载或导致导航的动作之后。

+0

谢谢你的帖子Sai。请参阅我的文章更新“我已经尝试的事实和事情”。它让我非常困扰,所有非无头浏览器都不需要WebDriverWait。他们对交互元素“只是工作”。为什么幽灵不同?另外,如果我必须在使用每个元素之前使用WebDriverWait,我不确定如何避免在等待调用时抛出代码。 – user1701153

+0

编辑答案包括一些细节。将浏览浏览器和驱动程序之间的更多差异,并保持发布。 – Sai