Selenium+java+firefox实现百度搜索。

40 篇文章 1 订阅

1.版本

        selenium

		<!--selenium-->
		<dependency>
			<groupId>io.github.bonigarcia</groupId>
			<artifactId>webdrivermanager</artifactId>
			<version>5.0.3</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.141.59</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-api</artifactId>
			<version>3.141.59</version>
		</dependency>

        geckodriver版本:0.29.1

        安装详见:selenium java安装运行_leadseczgw01的博客-CSDN博客

2.实现百度搜索

    public String baiduSearch(String profileName, String searchKeyword) throws Exception{
        WebDriver driver = getWebDriver(profileName);

        // 打开网址
        driver.get("https://www.baidu.com/");

        Thread.sleep(1000 * 3);
        SeleniumFirefoxUtil.detectAndCloseLoginTip(driver); // 探测并关闭登录提示

        // 找到搜索文本框
        WebElement webElement = driver.findElement(By.id("kw"));
        webElement.sendKeys(searchKeyword);

        // 找到搜索点击按钮,并点击
        webElement = driver.findElement(By.id("su"));
        webElement.click();

        Thread.sleep(1000 * 3);
        webElement = driver.findElement(By.xpath("//div[@id='tsn_inner']/div[2]/span"));
        String btnText = webElement.getText();
        logger.debug("搜索结果:{}!", btnText);

        // 退出浏览器
        driver.quit();
        return btnText;
    }

    private WebDriver getWebDriver(String profileName) {
        try{
            // 指定火狐浏览器安装位置
            System.setProperty("webdriver.firefox.bin", firefoxBin);
            // 指定selenium 火狐浏览器驱动程序位置
            System.setProperty("webdriver.gecko.driver", driverPath);

            // 获取火狐浏览器驱动对象
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setHeadless(true);
            firefoxOptions.addArguments("--no-sandbox");
            firefoxOptions.addArguments("--disable-gpu");
            firefoxOptions.addArguments("--disable-dev-shm-usage");

            if(StringUtils.isNotBlank(profileName)){
                String profilePath = firefoxProfileHome + File.separator + profileName;
                logger.debug("profilePath:{}", profilePath);
                File profileFile = new File(profilePath);
                if(!profileFile.exists()){
                    logger.error("profilePath:{}不存在或权限不足!", profilePath);
                }else{
                    FirefoxProfile profile = new FirefoxProfile(profileFile);
                    firefoxOptions.setProfile(profile);
                }
            }

            FirefoxDriver firefoxDriver = new FirefoxDriver(firefoxOptions);
            firefoxDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
            firefoxDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

            return firefoxDriver;
        }catch (Exception e){
            logger.error("获取webDriver异常!", e);
        }
        return null;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值