selenium
Java로 셀렌의 첫 번째 프로젝트
수색…
소개
이것은 Java를 사용하여 Selenium에 대한 소개입니다. Selenium에 관해서는 알지 못하지만,이 과정을 수강하기 위해서는 Java에 대한 사전 지식이 필요합니다.
링크 다운로드 :
Selenium을위한 IntelliJ 아이디어 설정
선수 과목 :
- 자바가 설치됨
- Selenium은 폴더에서 추출됩니다 (2 개의 파일과 1 개의 폴더 포함).
다음 단계에 따라 Selenium 용 IntelliJ Idea를 설정하십시오.
- "새 프로젝트"를 클릭하십시오.
- Java < "Hello World"응용 프로그램 선택
- 프로젝트 이름을 입력하고 만듭니다.
자, 가라.
File < Project Structure < Modules < Dependencies
거기에서 녹색 더하기 ( +
) 아이콘을 클릭하고 라이브러리를 선택하십시오. 그런 다음 압축을 푼 Selenium 폴더로 이동하여 " selenium-java 2.4.0.jar "를 추가하십시오. 추가 후 녹색 플러스 ( +
) 아이콘을 다시 클릭하고 "디렉토리"를 선택하십시오 . 이번에는 Selenium의 libs 폴더를 찾은 다음 ok를 클릭하십시오.
이제 확인을 클릭하면 모든 설정이 완료됩니다.
ChromeDriver 설정
선수 조건 : ChromeDriver 다운로드
다음 코드를 클래스에 복사하십시오.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
}
linux를 사용하는 경우 ChromeDriver Binary 경로를 지정합니다.
Selenium을 사용하여 웹 사이트 열기
get
메소드를 사용하여 웹 사이트로 이동합니다. 예를 들어, 이것은 google을 열 것이다.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https:www.google.com");
Thread.sleep(3000); //wait for 3 seconds
driver.quit(); //close Chrome
}
driver.quit()
는 브라우저를 닫습니다. 지연을 만들려면 Thread.sleep(3000)
합니다.
Selenium에서 요소 가져 오기
Selenium의 모든 Html 요소는 WebElement
라고합니다. 예를 들어, p
태그는 WebElement
이고, a
태그는 WebElement
. 다음 HTML 구조를 고려하십시오.
<a id="link1" href="https://www.google.com">google</a>
<p class="p1">
This is a paragraph
</p>
우리가 도착하기를 원한다면 이제 태그를, 우리가 할 수있는 a
WebElement link = driver.findElement(By.id("link1"));
이제이 버튼을 클릭 할 수 있습니다.
link.click();
또 다른 예를 들어 보겠습니다. p
태그의 텍스트, 즉 " This is a paragraph "를 원한다면 우리는 할 수 있습니다.
WebElement p = driver.findElement(By.className("p1"));
System.out.println(p.getText());
또한 요소를 태그 (예 : 태그)로 가져올 수 있습니다.
WebElement tag = driver.findElement(By.tagName("a"));
셀레늄의 실제 예제
이제 셀렌의 기본 사항을 알았으므로 자체 프로젝트를 만들 수 있습니다. 이 예제에서는 stack-overflow에 관한 가장 최근의 질문을 찾는 프로그램을 만들 것입니다.
우리는 쉽게 시작하고 스택 오버플로를 허용합니다.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https:stackoverflow.com");
Thread.sleep(3000);
driver.quit();
}
이제 페이지의 소스를 보면 모든 질문이 className의 question-hyperlink
a
태그임을 알 수 있습니다. 여러 질문이 있습니다 때문에, 우리는 사용 List
의 WebElement
대신, WebElement
. 따라서 우리는 할 수있다.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path to chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https:stackoverflow.com");
List<WebElement> list = driver.findElements(By.className("question-hyperlink"));
}
이제 우리는 질문의 링크가있는 a
태그의 href
속성을 href
합니다. 이를 위해 각 WebElement
에서 getAttribute("href")
를 사용할 수 있습니다.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path to chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https:stackoverflow.com");
List<WebElement> list = driver.findElements(By.className("question-hyperlink"));
System.out.println(list.size());
list.forEach(e->System.out.println(e.getAttribute("href")));
driver.quit();
}
Stack-overflow에 대한 상위 질문의 링크를 인쇄합니다.
Selenium에서 WebElement의 속성 가져 오기
a의 속성을 얻으려면 WebElement
, 우리는 사용 getAttribute
그에 WebElement
. 예를 들어, 다음 HTML 태그를 고려해보십시오.
<a id="click" href="https://www.google.com">
Element의 href
속성은 다음과 같이 찾을 수 있습니다.
WebElement e = driver.findElement(By.id("click"));
System.out.println(e.getAttribute("href")); //prints https://www.google.com