Java selenium 超时页面 [英] Java selenium timeout page

查看:71
本文介绍了Java selenium 超时页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 selenium 制作一个代理检查器,如果加载时间超过 15 秒,有什么办法可以让它进入下一个代理吗?

I'm making an proxy checker in selenium, is there any way I can make it go onto the next proxy if it takes more then 15 seconds too load?

所以,如果它尝试第一个代理,加载需要 15 秒或更长时间,只需移动到下一个代理

So, if it tries the frist proxy, it takes 15 or more seconds to load, just move onto the next proxy

package a;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.*;

public class main {

    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedReader br = new BufferedReader(new FileReader("proxy.txt"));
        String line;
        BufferedWriter file = null;
        file = new BufferedWriter(new FileWriter("proxy_out.txt"));


        while ((line = br.readLine()) != null) {
            //splitting
            String str;
            str = line;
            String[] splited = line.split(":");
            //System.out.println(Arrays.toString(splited));
            String IP = splited[0];
            String PORT = splited[1];
            System.out.println(IP + ":" + PORT);


            String PROXY = IP + ":" + PORT;
            org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
            proxy.setHttpProxy(PROXY)
                    .setFtpProxy(PROXY)
                    .setSslProxy(PROXY);
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setCapability(CapabilityType.PROXY, proxy);

            WebDriver driver = new ChromeDriver(cap);


            driver.get("google.com");
            try {
                WebElement Check = driver.findElement(By.id("input_captcha"));
                if (Check.isDisplayed()) {
                    System.out.println(PROXY + " is good");
                    file.write(IP+":"+PORT);
                }
            } catch (Exception e) {
                System.out.println("Bad");

            }
            driver.close();
        }
        file.close();
    }
}

推荐答案

您可以尝试使用 WebDriverWait 类,如下所示.

You may try using WebDriverWait class like below.

WebDriverWait wait = new WebDriverWait(driver, 15); //Timeout is 15 seconds
WebElement element = wait.until(ExpectedConditions.elementToBeSelected(By.id("input_captcha")));

此代码将不断尝试查找 input_captcha 元素,直到找到元素或达到超时

This code will continually try to find the input_captcha element, until either the element is found or the timeout is reached

这篇关于Java selenium 超时页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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