我搜索一個文本框,然後嘗試用一個字符串填充它。下面的代碼:PHantom JS失敗50%的時間
var fname = _driver.FindElement(By.Name("iFirstName"), 50);
if(fname!=null)
{
do
{
System.Threading.Thread.Sleep(500);
} while (!fname.Displayed);
fname.SendKeys(myName);
}
的FindElement功能是這樣的:
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
一些文本充滿字符串的時間,其他時間我得到這個錯誤:
System.InvalidOperationException: {"errorMessage":"'undefined' is not an object (evaluating '(y(a)?y(a).parentWindow||y(a).defaultView:window).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close","Host":"localhost:59868"},"httpVersion":"1.1","method":"GET","url":"/displayed","urlParsed":{"anchor":"","query":"","file":"displayed","directory":"/","path":"/displayed","relative":"/displayed","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/displayed","queryKey":{},"chunks":["displayed"]},"urlOriginal":"/session/af970250-310e-11e4-8996-210a8c2c5f2a/element/%3Awdc%3A1409489997045/displayed"}}
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:line 1048
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:line 865
at OpenQA.Selenium.Remote.RemoteWebElement.get_Displayed() in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebElement.cs:line 187
這裏有什麼問題?我甚至在調用所有上述函數之前創建一個打印屏幕,並且所有元素都被正確繪製,因此該頁面被正確加載。
此問題已在PhantomJS 1.9.7中發現(請參閱https://github.com/detro/ghostdriver/issues/325),並仍存在於PhantomJS 1.9.8中。 – Stephan 2015-01-12 00:17:47