selenium 크롬 자동화 할 때 현재 실행중인 크롬에서 실행하기 > vb.net

본문 바로가기

vb.net

[Chrome] selenium 크롬 자동화 할 때 현재 실행중인 크롬에서 실행하기

회원사진
하나를하더라도최선을
2022-09-24 21:38 3,327 0

본문



자동화를 하면 항상 새 크롬창을 띄웠는데 현재 실행 중인 크롬 창에서 실행할 수는 없을까해서 검색해본 결과


무조건 한번은 새로 크롬을 새로 띄워야 한다는 결론에 도달.


그리고 그 크롬에서 적용 할 수 있었다.


첫번째 해야할 것은 (크롬드라이버 다운은 필수)


cmd에서 디버그용? 크롬을 실행시켜야 한다


chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile" 


이걸 cmd에서 실행해보면 크롬이 실행된다.


안된다면 C:\Program Files (x86)\Google\Chrome\Application\ 이 경로로 이동해서 실행해보거나


환경변수에 위 경로를 추가해준다.



이 크롬으로 계속 재활용할 수 있다.


이제 이걸 자바 코드 내에 cmd 실행을 시킨다음 실행해주면 된다.


참고로 코드 내에서 실행할 때는 완전한 경로로 해주어야 실행이 된다.


 Runtime.getRuntime().exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=\"C:/selenum/AutomationProfile\"");


위 코드를 먼저 실행해 준 다음


ChromeOptions options = new ChromeOptions();

options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");

driver = new ChromeDriver(options); 


크롬 옵션을 추가해주어 실행된 크롬창을 사용하도록 지정해준다.(9222포트는 위 실행명령어 포트와 동일)



전체적인 코드

 

public class Automation {
 
    private static WebDriver driver;
 
    @BeforeClass 
    public static void setUp() throws Exception { 
        System.setProperty("webdriver.chrome.driver""C:\\chromedriver.exe");
            
        Runtime.getRuntime().exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=\"C:/selenum/AutomationProfile\"");
 
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress""127.0.0.1:9222");
        driver = new ChromeDriver(options);
        
        System.out.println(driver.getTitle());
        driver.manage().window().maximize();
        driver.get("https://www.naver.com");
        
    }
}




 자동화 실행 후 현재 실행중인 크롬에서 확인해보려면  Runtime.getRuntime().exec 코드를 주석처리하고 실행하면 확인해볼 수 있다


주석처리를 안하면 실행할 때마다 새로운 크롬이 생성된다.

출처: h t t p s : / / y o n o o 8 8 . t i s t o r y . c o m / 1 2 3 7 [Blog:티스토리]

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색