以下代码在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
// 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 & 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>
你可以尝试使用不同的选择器来查找元素。 – Abhinav
另外,你可以尝试使用最大化浏览器:driver.manage()。window()。maximize();我看到几个帖子称这是解决方法。请让知道,如果有帮助 – Abhinav
感谢您的建议Abhinav。我已经更新了“我试过的事实和事情”的帖子。很遗憾,您的建议无法解决问题。 – user1701153