java 启动Selenium 以及端口占用的问题

2023-11-11



ERROR [BaseServer.start] - Port 4444 is busy, please choose a free port and specify it using -port..


开启Selenium server

问题描述
例如:使用命令java -jar .\src\main\resources\driver\selenium-server-standalone-3.141.59.jar出现报错如下:


原因分析:
显示端口4444被占用,解决方法要么停掉端口,要么换成其他端口

 

解决方案:
我这边采用的是第2种方法,切换为8090端口
命令:java -jar .\src\main\resources\driver\selenium-server-standalone-3.141.59.jar -port 4445
开启Selenium server成功!

 

Java运行web自动化代码1

package com.test.demo.web;

import com.test.pages.SearchPages;
import com.test.utils.Config;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.Properties;

/**
 * @Author: xinyi
 * @Description:
 * @Date: Created In 9:11 2018/8/3
 * @Modified By:
 */
public class TestDemo {
    WebDriver driver;

    public static void main(String[] args) {
//        test();
    }

//    @Test
//    public static void test(){
//        String url = "http://www.baidu.com";
//        WebDriver driver;
//        //新建一个浏览器句柄
//        driver = new Brower().chrome();
//        //打开URL
//        driver.get(url);
//        //输入搜索字符串
//        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("pYTHON");
//        //点击[百度一下]按钮
//        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    @BeforeTest
    public void init() {
        String url = "http://www.baidu.com";

        //新建一个浏览器句柄
        driver = new Brower().chrome();
        //打开URL
        driver.get(url);
    }

    @Test
    public void testSearch(){
        Browser browser = new Browser();
        Properties properties = Config.getProperties();
        WebDriver driver = browser.openUrl(properties.getProperty("url"));
        SearchPages search = new SearchPages();
        search.input(driver, "12306");
        search.search(driver);
        driver.close();
    }
    @Test
    @Parameters("searchWord")
    public void search(String searchWord) throws InterruptedException {
        //输入搜索字符串
        System.out.println("searchWord: "+searchWord);
        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(searchWord);
        //点击[百度一下]按钮
        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
        Thread.sleep(2000);
    }

    @AfterTest
    public void teardown() {
        driver.quit();
    }

}
 

Java运行web自动化代码2

package com.test.demo.web;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;

import java.util.Properties;

/**
 * @author 作者:微信搜索关注【测试工程师成长之路】公众号
 * @version 创建时间:2020/12/5
 * 类说明:打开浏览器
 */
public class OpenBrowser {

    public WebDriver driver;

    @Test
    public void test() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--kiosk");//MAC OS下浏览器最大化
        Properties props = System.getProperties();
        String systemOS = props.getProperty("os.name");
        if(systemOS.contains("Mac")){
            // 设置驱动属性,驱动存放在drivers路径下
            System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
            driver = new ChromeDriver(options);
        }else{
            System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
            driver = new ChromeDriver();
        }
        // Chrome浏览器打开cnblogs
        driver.get("https://www.cnblogs.com/mrjade");
        Thread.sleep(3000);
        if (!"Mac".contains(systemOS)) {
            // win下浏览器最大化
            driver.manage().window().maximize();
        }
        // 关闭浏览器
        driver.close();
        // 关闭浏览器驱动
        driver.quit();
    }

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java 启动Selenium 以及端口占用的问题 的相关文章

随机推荐