gpt4 book ai didi

java - selenium 等待代理页面加载超时

转载 作者:行者123 更新时间:2023-12-01 11:06:05 26 4
gpt4 key购买 nike

我使用代理连接到使用 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/

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