Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

Java selenium timeout page

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?

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")));

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...