Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...