gpt4 book ai didi

c# - PHAntom JS 有 50% 的时间失败

转载 作者:太空狗 更新时间:2023-10-30 00:51:41 25 4
gpt4 key购买 nike

我搜索一个文本框,然后尝试用一个字符串填充它。这是代码:

    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

这里有什么问题?我什至在调用上述所有函数之前制作了一个打印屏幕,并且所有元素都被正确绘制,因此页面被正确加载。

最佳答案

问题是我设置了一个从列表中随机生成的自定义 UserAgent,其中一些 UserAgent(Internet Explorer 的)导致了错误:

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

我删除了它们,现在我不再得到异常。

关于c# - PHAntom JS 有 50% 的时间失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com