gpt4 book ai didi

Java Selenium 超时页面

转载 作者:行者123 更新时间:2023-12-01 09:40:14 25 4
gpt4 key购买 nike

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

因此,如果它尝试第一个代理,则需要 15 秒或更长时间来加载,只需移至下一个代理

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 类。

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

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

关于Java Selenium 超时页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38513338/

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