selenium设置代理ip

来源:互联网 发布:网络短剧 搞笑 编辑:程序博客网 时间:2024/05/01 14:44
package com.fahai;


import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class SeleniumProxyIp {

private static String windowHandle;
public static void main(String[] args) throws InterruptedException {

//weile防止服务器封锁,这里的时间要模拟人的行为,随机且不能太短
long waitLoadBaseTime = 3000;
int waitLoadRandomTime = 3000;
Random random = new Random(System.currentTimeMillis());
//火狐浏览器
System.setProperty("webdriver.firefox.bin", "F:\\Firefox Setup 32.0.1\\firefox.exe");

//使用代理ip测试
String proxyIp = "139.59.211.210";
int proxyPort = 8080;

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", proxyIp);
profile.setPreference("network.proxy.ssl_port", proxyPort);

//所有协议公用一种代理配置,如果单独配置,这项设置为false
profile.setPreference("network.proxy.share_proxy_settings", true);

WebDriver driver = new FirefoxDriver(profile);
// driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

driver.get("http://www.yidianzixun.com/home?page=channel&keyword=%E8%8A%B1%E8%8C%B6");
//等待页面动态加载完毕
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));

int pages = 1;
for(int i=1;i<2;i++){
driver.findElement(By.className("show-more")).click();

//等待页面加载完成
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
}

List<WebElement> findElements = driver.findElements(By.className("article"));

System.out.println("article的数量"+findElements.size());

WebElement webElement = findElements.get(0);

// for (WebElement webElement : findElements) {

WebDriver window;
//获取当前窗口句柄
windowHandle = driver.getWindowHandle();

WebElement link = webElement.findElement(By.tagName("h3")).findElement(By.tagName("a"));

System.out.println("link:"+link.getAttribute("href"));

//某个链接点击不到的原因
//1.未加载完成,增加等待时间
//2.被父节点隐藏,通过修改爬point(x.y)矢量积,使用链接能被模拟浏览器点击到

link.click();

//获得所有窗口的句柄
Set<String> handles = driver.getWindowHandles();

for (String string : handles) {
if (string.equals(windowHandle)) {
continue;
}else {
window = driver.switchTo().window(string);
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

//获取详细页面的内容
//因为有些链接是重定向直接跳转xpath格式不匹配
//抛出异常
try {
String text = window.findElement(By.xpath("//div[@class='content-hd-media']/h2")).getText();
System.out.println(text);

} catch (Exception e) {
e. printStackTrace();
}
//关闭窗口,其实也是该窗口句柄被rush
window.close();
}
//切换到主窗口
driver.switchTo().window(windowHandle);
}

Thread.sleep(1000);

driver.close();//关闭浏览器
// }
}
}
0 0
原创粉丝点击