Watir-WebDriver HTTP 代码

发布于 2024-12-14 05:12:34 字数 1228 浏览 1 评论 0原文

所以我在 Firefox 和 Chrome 中使用 Watir WebDriver。 Chrome 速度更快,但似乎有一些怪癖。首先,在 Firefox 中,当我使用类似的东西时:

ff.link(:text, "Click Here").exists?

它工作正常。然而,在 Chrome 中,它似乎经常停滞,然后因超时异常而退出。因此,我必须做的就是缩小 Chrome 中的搜索范围,如下所示:

linkDiv = ch.div(:class, "mydiv")
linkDiv.link(:text, "Click Here").exists?

通常,这种类型的页面缩减到仅感兴趣的 div 标签效果很好。

然而,我在 Chrome 中注意到了另外一个怪癖,而在 Firefox 中却没有注意到。在 Chrome 中,我偶尔会收到错误 15 - 就像套接字上没有响应一样,但由于它是间歇性的,我无法随意重现该错误,并且还没有复制确切的文本。我还没有在 Firefox 中看到这一点 - 至少还没有 - 但也许 Chrome 给了我一个 Firefox 隐藏它的错误。

因此,我希望能够在发生此错误时进行记录,到目前为止,我最好的想法是使用 HTTP 代码 - 即 200、404、500 等。如果代码不是 200,则记录代码并再试一次。

Watir 有没有办法获取 HTTP 代码?从 PHP/CURL 转向 Ruby 后,我假设有一种简单的方法来获取 HTTP 代码 - 类似 browser.http_code (其中 browser = Watir::Browser.new :chrome, browser.goto (...)) 但我还没有找到它,而且我也没有运气在网上找到方法。到目前为止,我发现的“解决方案”都需要其他 gem 调用页面以获取返回的代码。然而,由于每次我收到此错误时,这些页面都可以正常工作 10 次或更多,因此使用另一个 gem 再次调用并没有帮助。

谢谢


我的解决方法...

上面没有任何内容的间歇性页面的标题在 browser.title 字符串末尾包含“不可用”。所以我使用 browser.title.include? “不可用” 检查页面是否从未加载。

由于多种原因,这不太理想,例如标题可能会因站点而异,或者可能会在 Chrome 升级时发生变化。状态代码 200 总是告诉我的代码有问题......

我想这是向开发人员建议的事情。

再次感谢

So I'm using Watir WebDriver with both Firefox and Chrome. Chrome is much faster but seems to have a number of eccentricities. First, in Firefox when I use something like:

ff.link(:text, "Click Here").exists?

It works fine. However, in Chrome it often seems to stall and then exit with a timeout exception. So what I've had to do is narrow the search in Chrome like this:

linkDiv = ch.div(:class, "mydiv")
linkDiv.link(:text, "Click Here").exists?

Typically this type of page reduction down to just the div tag of interest works just fine.

However, there is one other eccentricity I've noticed in Chrome that I haven't noticed in Firefox. In Chrome, I occasionally get an Error 15 - something like no response on socket but since it is intermittent I can't reproduce the error at will and haven't copied the exact text down yet. I haven't seen this in Firefox - at least not yet - but perhaps Chrome is giving me an error where Firefox hides it.

So I'd liked to be able to log when this error occurs and the best idea I've had so far is to use HTTP Codes - i.e. 200, 404, 500, etc. If the code is not 200 then record the code and try again.

Is there a way in Watir to get the HTTP code? Having come to Ruby from PHP/CURL I assumed there would be an easy way to get the HTTP code - something like browser.http_code (where browser = Watir::Browser.new :chrome, browser.goto(...)) but I haven't found it yet and I haven't had any luck finding a way online. The "solutions" I've found so far all entail other gems making a call to the page to get the returned code. However, since these pages work fine 10 times or more for every time I get this error making another call with another gem doesn't help.

Thanks


My workaround...

The title of the intermittent page with nothing on it includes "is not available" at the end of the browser.title string. So I use a browser.title.include? "is not available" check to see if the page never loaded.

This is less than ideal for a lot of reasons like that title may change from site to site or may change when Chrome gets upgraded. Where as a status code of 200 will always tell my code there is a problem...

I guess it's something to suggest to the developers.

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-12-21 05:12:35

您无法使用 Watir-Webdriver 来做到这一点。请参阅https://stackoverflow.com/a/6512785/418107

You can't do it with Watir-Webdriver. See https://stackoverflow.com/a/6512785/418107.

吹泡泡o 2024-12-21 05:12:35

你尝试过吗?

browser.status

另一件事是按照用户体验的方式进行检查,通过查看结果页面,检查标题或文本以查看是否指示错误。

如果这不是您想要的,那么除了 watir 之外,您还需要使用 HTTP 级别的 gem

请参阅此问题和答案:使用 Watir 检查不良链接

Have you tried?

browser.status

The other thing would be to check on it the way the user would experience it, by looking at the resulting page, checking the title or text to see if it indicates an error.

If that's not what you are looking for, then you will need to use an HTTP level gem in addition to watir

See this SO question and answer: Using Watir to check for bad links

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文