zoukankan      html  css  js  c++  java
  • selenium之调用js解决淘宝点击下一页问题(JAVA版)

    最近研究点击淘宝商品底部 点击“下一页”问题。今天经网友指导得解,感谢网友且听风吟和燊!

    分析原因:没加载上和定位问题

    解决思路:用js将浏览器滚动到底部,等待加载完,再进行点击

    解决代码

    package wxLinkParaser;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class click {
        public boolean click(){
            System.getProperties().setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
            WebDriver webDriver = new ChromeDriver();
           
            try{
                webDriver.get("http://list.taobao.com/itemlist/default.htm?cat=50000671&viewIndex=1&as=0&atype=b&style=grid&same_info=1&isnew=2&tid=0&_input_charset=utf-8");
                try{
                    Thread.sleep(5000); // 等待浏览器加载
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                webDriver.manage().window().maximize(); // 使浏览器窗口最大化
    
                //js代码
                String js1 = "return document.documentElement.scrollHeight;";  // 滑动条的位置值
                String js2 = "scroll(0,10000);";
                
                ((JavascriptExecutor)webDriver).executeScript(js2);//执行js代码,返回浏览器滚动高度
                try{
                    Thread.sleep(3000); //等待浏览器加载
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ((JavascriptExecutor)webDriver).executeScript(js1);//执行js代码,滚动10000像素
                
                
                try{
                    Thread.sleep(3000); // wait for web loading
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }  
                
                 WebElement Whref = webDriver.findElement(By.cssSelector("a.J_Ajax.btn.next"));//得到要点击的“下一页”链接
                
                 Whref.click();//进行点击
                 //webDriver.close();这句先注视掉,以便观察结果
                 return true;
            }catch(org.openqa.selenium.NoSuchElementException ex){
                System.out.println("找不到要的下一页链接!");
                //webDriver.close();这句先注视掉,以便观察结果
                return false;
            }            
            
        }
            
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            click c = new click();
            if(c.click()){
                System.out.println("点击成功!");
            }
        }
    
    }
  • 相关阅读:
    FPGA时序约束的几种方法
    使用NiosII代替SignalTap来监测FPGA内部数据
    Modelsim的使用
    Modelsim+Debussy
    ChipScope用法总结
    QuartusII增量编译的个人学习
    quartus II .qsf文件(zz)
    RAM与Nand/Nor flash之间的区别 (转)
    黑金资料AX301_A的Quartus工程建立、编译及引脚分配、程序下载
    关于sg90舵机的,要知道!要注意!
  • 原文地址:https://www.cnblogs.com/Rosefxd/p/3655844.html
Copyright © 2011-2022 走看看