- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用代理连接到使用 Selenium 的网站并测试一些东西,问题是一些代理非常慢,这使得事情效率很低,但另一个问题是我无法捕获错误并进行无论我做什么..尝试了其他堆栈帖子中的各种建议,但无济于事。
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(ip+":"+port);
proxy.setSslProxy(ip+":"+port);
proxy.setFtpProxy(ip+":"+port);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(dc);
try {
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
driver.navigate().to("http://foo.bar");
} catch (Exception ex) {
ex.printStackTrace();
driver.quit();
driver = null;
break;
}
我将超时设置为 15 秒,因为它必须浏览几个页面,并且某些代理需要 30-40 秒才能加载页面,因此需要的时间要长得多,实际上有没有办法捕获错误
Timed out waiting for page load
我的另一个问题是,使用 selenium 的替代品会更容易吗?到目前为止,我对 Selenium 有一些问题,从其他人的帖子来看,这些问题似乎已经存在了一段时间
最佳答案
脚本,
在我看来,您需要 ExplicitWaits 来验证您的导航。 ExplicitWaits 不是在驱动程序中使用隐式配置,而是根据您的环境提供更灵活的解决方案。根据我的经验,WebDriverWait 类与 ExpectedConditions 相结合已经解决了我的大部分问题。我还没有获得对驱动程序的代理支持,但它似乎与我迄今为止获得的信息一致。
话虽这么说,我不完全确定为什么你没有看到抛出异常。当我遇到类似问题时,我会将 WebDriver 包装在 EventFiringWebDriver 中,并查看在这些情况下 WebDriverEventListener 的 onException 消息是否会被命中。
在下面的示例中,您需要填写代理 IP 和端口的常量,以及您想要访问的实际 URL。在 createParameters 方法中,您还需要更新标题和部分 url 的字符串以适合您的用例。
使用 Java 8 和 Selenium v 2.47.2 构建
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.collect.Lists;
@RunWith(Parameterized.class)
public class ExplicitWaitNavigationTest {
/** IP configuration for the proxy.*/
private static final String PROXY_IP = "";
/** Port configuration for the proxy.*/
private static final int PROXY_PORT = 8080;
/** The url the test will navigate to.*/
private static final String TEST_URL = "http://the-internet.herokuapp.com/dropdown";
/** Maximum time to wait for a page to load.*/
private static final int PAGE_LOAD_SECONDS = 15;
/** Maximum time to wait for javascript to complete.*/
private static final int SCRIPT_LOAD_SECONDS = 15;
/** ExpectedCondition used to validate the navigation state.*/
private ExpectedCondition<Boolean> navTestCondition;
/** The URL to be tested in this instance.*/
private String url;
/** WebDriver reference used for testing navigation through proxy.*/
private WebDriver webDriver;
/**
* Assembles the Constructor arguments for testing.
* Due to test parameterization, each pair is run as a separate test.
* @return Object array of test parameters.
*/
@Parameters
public static Object[] createParameters() {
List<Object[]> params = Lists.newArrayList();
String url = TEST_URL;
//Test that url exactly matches
ExpectedCondition<Boolean> test = ExpectedConditions.urlToBe(url);
params.add(new Object[]{test, url});
//Test if the url matches a regex?
test = ExpectedConditions.urlMatches(url); //regex match? I'm not great with regex
params.add(new Object[]{test, url});
//Test if the url contains the base site, or other fragment information
test = ExpectedConditions.urlContains("dropdown");
params.add(new Object[]{test, url});
// Test if the page title exactly matches expectation.
test = ExpectedConditions.titleIs("The Internet");
params.add(new Object[]{test, url});
//Test if the page title somewhat matches expectation
test = ExpectedConditions.titleContains("Internet");
params.add(new Object[]{test, url});
return params.toArray(new Object[]{});
}
/**
* Turns off noisy logging that is on by default in the Selenium structure.
*/
@BeforeClass
public static void disableHttpUnitOutput() {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
}
/**
* Constructor.
* @param waitCondition Expected condition to validate in this effort.
* @param url URL being navigated to.
*/
public ExplicitWaitNavigationTest(ExpectedCondition<Boolean> waitCondition, String url) {
this.navTestCondition = waitCondition;
this.url = url;
}
/**
* Configures the Driver for this test instance.
*/
@Before
public void setupDriver() {
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
String proxyConfig = PROXY_IP + ":" + PROXY_PORT;
proxy.setHttpProxy(proxyConfig);
proxy.setSslProxy(proxyConfig);
proxy.setFtpProxy(proxyConfig);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.PROXY, proxy);
FirefoxDriver ffd = new FirefoxDriver(dc);
ffd.manage().timeouts().pageLoadTimeout(PAGE_LOAD_SECONDS, TimeUnit.SECONDS);
ffd.manage().timeouts().setScriptTimeout(SCRIPT_LOAD_SECONDS, TimeUnit.SECONDS);
//XXX EventFiringWebDriver decoration
EventFiringWebDriver efwd = new EventFiringWebDriver(ffd);
efwd.register(new AbstractWebDriverEventListener() {
@Override
public void onException(Throwable throwable, WebDriver driver) {
System.out.println("Exception thrown from within the EventFiringWebDriver");
throwable.printStackTrace();
}
@Override
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("Before NavigateTo :: " + url);
}
@Override
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("After NavigateTo :: " + url);
}
});
webDriver = efwd;
}
/**
* Test instance which attempts to navigate to the configured url and establishes an ExplicitWait to confirm
* navigation before continuing with the test event.
*/
@Test
public void testPageLoadFeedback() {
webDriver.navigate().to(url);
/*
* FIXME: How long are you actually willing to wait.
*
* It's my understanding that if the wait value is less than the implicit wait, then the looping capabilities of
* the ExplicitWait are never really leveraged.
*
* Note that the time value is the maximum time to wait. If the condition validates before the time specified
* then the process will continue.
*/
WebDriverWait wait = new WebDriverWait(webDriver, PAGE_LOAD_SECONDS * 2);
wait.pollingEvery(500, TimeUnit.MILLISECONDS);
wait.until(navTestCondition);
// If we get to this line then the test passed and we should be on the page.
System.out.println("Navigation Succeeded!");
}
/**
* Cleanup method to close any browsers used by the test session.
*/
@After
public void cleanupTest() {
//XXX: To leave the browser open, comment out the @After of this method.
for (String handle : webDriver.getWindowHandles()) {
webDriver.switchTo().window(handle);
webDriver.close();
}
}
}
祝你好运,希望这会有所帮助。
关于java - selenium 等待代理页面加载超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934845/
我想要显示正在加载的 .gif,直到所有内容都已加载,包括嵌入的 iframe。但是,目前加载 gif 会在除 iframe 之外的所有内容都已加载后消失。我怎样才能让它等到 iframe 也加载完毕
首先,这是我第一次接触 Angular。 我想要实现的是,我有一个通知列表,我必须以某种方式限制 limitTo,因此元素被限制为三个,在我单击按钮后,其余的应该加载。 我不明白该怎么做: 设置“ V
我正在尝试在我的设备上运行这个非常简单的应用程序(使用 map API V2),并且出于某种原因尝试使用 MapView 时: 使用 java 文件: public class MainMap e
我正在使用 Python 2.6、Excel 2007 Professional 和最新版本的 PyXLL。在 PyXLL 中加载具有 import scipy 抛出异常,模块未加载。有没有人能够在
我想做这个: 创建并打包原始游戏。然后我想根据原始游戏中的蓝图创建具有新网格/声音/动画和蓝图的其他 PAK 文件。原始游戏不应该知道有关其他网格/动画/等的任何信息。因此,我需要在原始游戏中使用 A
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。 本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在 Groovy 脚本中是否可以执行条件导入语句? if (test){ import this.package.class } else { import that.package.
我正在使用 NVidia 视觉分析器(来自 CUDA 5.0 beta 版本的基于 eclipse 的版本)和 Fermi 板,我不了解其中两个性能指标: 全局加载/存储效率表示实际内存事务数与请求事
有没有办法在通过 routeProvider 加载特定 View 时清除 Angular JS 存储的历史记录? ? 我正在使用 Angular 创建一个公共(public)安装,并且历史会积累很多,
使用 Xcode 4.2,在我的应用程序中, View 加载由 segue 事件触发。 在 View Controller 中首先调用什么方法? -(void) viewWillAppear:(BOO
我在某些Django模型中使用JSONField,并希望将此数据从Oracle迁移到Postgres。 到目前为止,当使用Django的dumpdata和loaddata命令时,我仍然没有运气来保持J
创建 Nib 时,我需要创建两种类型:WindowNib 或 ViewNib。我看到的区别是,窗口 Nib 有一个窗口和一个 View 。 如何将 View Nib 加载到另一个窗口中?我是否必须创建
我想将多个env.variables转换为静态结构。 我可以手动进行: Env { is_development: env::var("IS_DEVELOPMENT")
正如我从一个测试用例中看到的:https://godbolt.org/z/K477q1 生成的程序集加载/存储原子松弛与普通变量相同:ldr 和 str 那么,宽松的原子变量和普通变量之间有什么区别吗
我有一个重定向到外部网站的按钮/链接,但是外部网站需要一些时间来加载。所以我想添加一个加载屏幕,以便外部页面在显示之前完全加载。我无法控制外部网站,并且外部网站具有同源策略,因此我无法在 iFrame
我正在尝试为我的应用程序开发一个Dockerfile,该文件在初始化后加载大量环境变量。不知何故,当我稍后执行以下命令时,这些变量是不可用的: docker exec -it container_na
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我刚刚遇到一个问题,我有一个带有一些不同选项的选择标签。 现在我想检查用户选择了哪些选项。 然后我想将一个新的 html 文件加载到该网站(取决于用户选中的选项)宽度 javascript,我该怎么做
我知道两种保存/加载应用程序设置的方法: 使用PersistentStore 使用文件系统(存储,因为 SDCard 是可选的) 我想知道您使用应用程序设置的做法是什么? 使用 PersistentS
我开始使用 Vulkan 时偶然发现了我的第一个问题。尝试创建调试报告回调时(验证层和调试扩展在我的英特尔 hd vulkan 驱动程序上可用,至少它是这么说的),它没有告诉我 vkCreateDeb
我是一名优秀的程序员,十分优秀!