엘리먼트를 찾을 때 권장하는 방법은 id나 name, class 속성을 이용해 엘리먼트의 위치를 지정하는 방식이다.
- driver.findElement(By.id(id));
WebElement username = driver.findElement(By.id("username"));
WedElement password = driver.findElement(By.id("password"));
- driver.findElement(By.name(name));
- 엘리먼트의 id 속성을 부여하지 않은 경우
- id 속성 값이 엘리먼트를 구별하는 키 역할을 할 수 없는 경우
- id 속성 값이 동적으로 생성되는 경우
WebElement username = driver.findElement(By.name("username"));
WedElement password = driver.findElement(By.name("password"));
id 속성화 달리 name 속성 값은 페이지 내에서 유일하지 않을 수 있다.
naem속성 값과 유사한 다른 엘리먼트가 검색될 수 있는데, 페이지에서 첫 번째로 검색된 엘리먼트가 선택돼 의도한 값과 다를 수 있다.
- driver.findElement(By.className(classname)
WedElement username = driver.findElement(By.className("username"));
webElement password = driver.findElement(By.className("password"));
- 자식 엘리먼트 찾기
WedElement div = driver.findElement(By.id("div1"));
WedElement topLink = div.findElement(By.linkText("top"));
WedElement topLink = driver.findElement(By.id("div1")).findElement(By.linkText("top"));
'개발 공부' 카테고리의 다른 글
Selenium 에서의 링크 위치 지정 Ver.Java (0) | 2023.06.02 |
---|---|
Selenium 에서의 엘리먼트 위치 지정 NO.3 Ver.Java (여러 엘리먼트 지정 예제) (0) | 2023.06.02 |
Selenium 에서의 엘리먼트 위치 지정 NO.1 Ver.Java (메소드 종류) (0) | 2023.06.02 |
Selenium 이란? (0) | 2023.06.02 |
SpringBoot 프로젝트 생성(intelliJ 유료버전) (0) | 2023.05.31 |