【问题标题】:selenium Timed out waiting for page load with proxys硒超时等待代理页面加载
【发布时间】:2015-10-04 14:58:02
【问题描述】:

我使用代理连接到一个使用 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 方面遇到了一些问题,从其他人的帖子来看,这些问题似乎已经存在了一段时间

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    脚本,

    在我看来,您需要 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();
            }
        }
        
    }
    

    祝你好运,我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-07-08
      • 2017-03-15
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多