selenium 代理

//org.openqa.selenium.WebDriverException: Element is not clickable at point

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;

public class YidianzixunSelenium {

	
	private static String currentWindow;
	
    public static void main(String[] args) throws Exception{

        //为了防止服务器封锁,这里的时间要模拟人的行为,随机且不能太短
        long waitLoadBaseTime = 3000;
        int waitLoadRandomTime = 3000;
        Random random = new Random(System.currentTimeMillis());

        //火狐浏览器
        System.getProperties().setProperty("webdriver.firefox.bin", "E:\\software\\firefox\\firefox.exe");
        
        /**
         * Ip代理 参数参考:http://www.kuaidaili.com/
         */
//        String proxyIp = "113.67.14.151";
//        int proxyPort = 9999;
//        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);
     
        WebDriver driver = new FirefoxDriver();
        
        //要抓取的网页
        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=0; i<pages; i++) {
            //滚动加载下一页
        	
            driver.findElement(By.className("show-more")).click();
            //等待页面动态加载完毕
            Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
        }
        
        List<WebElement> elements = driver.findElements(By.className("article"));
           
        System.out.println("link size:"+elements.size());
        for(WebElement element:elements){
        	
        	WebDriver window;
        	
        	//当前driver所在的窗口句柄
            currentWindow = driver.getWindowHandle();
            
            WebElement link = element.findElement(By.tagName("h3")).findElement(By.tagName("a"));
            
            System.out.println("link:"+link.getAttribute("href"));
        	
            //某个链接 点击不到 可能的原因
            //1)未加载完成,增加等待时间
            //2)被父节点隐藏,通过修改Point(x,y)矢量积,使链接能被模拟浏览器点击到
            try {
            	link.click();
			} catch (Exception e) {
				e.printStackTrace();
			}
            
            
            //获得所有的窗口句柄
            Set<String> handles= driver.getWindowHandles();
            
            for (String s : handles) {
                //current page is don't close
                if (s.equals(currentWindow))
                    continue;
                else 
                {	
                	//切换到详细信息的窗口
                    window =driver.switchTo().window(s);
                    window.manage().window().maximize();
                    window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                    window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
                    
                    //获取详细页的内容
                    //因为有些链接是重定向直接跳转  xpath格式不匹配 
                    //抛出异常
                    try {
                    	String title = window.findElement(By.xpath("//div[@class='content-hd']/h2")).getText();
                    	System.out.println(title);
					} catch (Exception e) {
						e.printStackTrace();
					}
                                        
                    }
                    //关闭窗口,其实也就是该窗口句柄被rush
                    window .close() ;
                }
            //切换到主窗口
            driver.switchTo().window(currentWindow);
          }  
        
        Thread.sleep(1000);
        
        
        //关闭浏览器
        driver.close();

    }

}


你可能感兴趣的:(js,selenium,点击,IP代理)