PHANTOM JS失败的50%的时间时间、PHANTOM、JS

2023-09-06 10:51:42 作者:走川

我搜索文本框,然后尝试用一个字符串填充它。这里的code:

I search for a textbox and then try to fill it with a string . Here's the code :

    var fname = _driver.FindElement(By.Name("iFirstName"), 50);
    if(fname!=null)
    {
        do
        {
            System.Threading.Thread.Sleep(500);
        } while (!fname.Displayed);
        fname.SendKeys(myName);
    }

该FindElement功能是这样的:

The FindElement function is this :

    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);
    }

部分的文本框充满了弦的时候,在其他时间我得到这个错误:

Some of the time the textbox is filled with the string, at other times I get this error :

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

有什么问题吗?我甚至做出PRINTSCREEN呼吁所有上述功能之前,所有的元素都正确地绘制,因此页面加载正确。

What's the issue here? I even make a printscreen before calling all the above functions and all the elements are drawn correctly therefore the page is correctly loaded.

推荐答案

但问题是,我设置一个自定义的UserAgent从列表中随机生成并且其中一些UserAgents(在Internet Explorer中的)引起的错误:

The problem was that I was setting a custom UserAgent generated randomly from a list and some of those UserAgents (the Internet Explorer ones) caused the errors:

        PhantomJSOptions options = new PhantomJSOptions();
        int header = GenerateRandomBetween(0,phantomHeader.Count-1);
        options.AddAdditionalCapability("phantomjs.page.settings.userAgent", phantomHeader[header]);

我把他们赶走,现在我不再得到的异常。

I removed them and now I no longer get the Exception.