如何“阻止"使用Selenium的AJAX响应 [英] How to "block" an AJAX response using Selenium

查看:40
本文介绍了如何“阻止"使用Selenium的AJAX响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总而言之,我要做的是打开"页面 driver.Navigate().GoToUrl("http://somepage.com"),然后立即阻止来自"http://somepage.com/something.asmx/GetStuff" 的响应,以便我可以在加载响应之前验证某些元素是否具有某些类:driver.FindElement(By.CssSelector("buttom.some-button")).GetAttribute("class").Contains("disabled"),然后是 disabled .

In summary, what I'm trying to do is "open" a page driver.Navigate().GoToUrl("http://somepage.com") and then immediately block the response from "http://somepage.com/something.asmx/GetStuff" so that I can verify that some element has some class before the response is loaded: driver.FindElement(By.CssSelector("buttom.some-button")).GetAttribute("class").Contains("disabled") and then the disabled.

是否有可能这样,如果可以,我该如何处理?

Is something like this possible, and if so, how do I go about it?

我的问题类似于没有Selenium Webdriver c#等待页面加载它要达到的目的.

My question is similar to Selenium Webdriver c# without waiting for page to load in what it's trying to achieve.

推荐答案

将您的IWebDriver实例(FirefoxDriver,ChromeDriver等)转换为 IJavacriptExecutor 并替换jQuery $.ajax()带有存根的方法,例如:

Cast your instance of IWebDriver (FirefoxDriver, ChromeDriver, etc) to IJavacriptExecutor and replace the jQuery $.ajax() method with a stub, such as:

var driver = Driver as IJavaScriptExecutor

driver.ExecuteScript("window.originalAjax = $.ajax")
driver.ExecuteScript("$.ajax = function() {}")

// navigate to page, check class

driver.ExecuteScript("$.ajax = window.originalAjax")

因此,当您的请求调用 $.ajax 时,它将打空方法.

So when your request calls into $.ajax it will hit a blank method.

这具有一个缺点,因为没有创建任何请求,因此您无法在阻止请求后轻易将其继续继续".您将必须刷新页面,而无需执行上述步骤,否则可能会导致误报.

This has the downside that you cannot easily get the request to 'continue' after blocking it, as no request was ever created. You would have to refresh the page without doing the above steps which could give some sort of false positive.

这篇关于如何“阻止"使用Selenium的AJAX响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆