selenium 튜토리얼
셀렌 시작하기
수색…
비고
Selenium은 프로그래머가 브라우저 상호 작용을 자동화 할 수있는 여러 언어 (C #, Haskell, Java, JavaScript, Objective-C, Perl, PHP, Python, R 및 Ruby)의 강력한 명령 라이브러리입니다. 이는 개발자가 응용 프로그램을 테스트하는 데 매우 유용합니다.
Selenium은 다음과 같은 방법을 제공합니다.
- 웹 페이지에서 요소 찾기
- 요소를 클릭하십시오.
- 요소에 문자열 보내기
- 웹 페이지로 이동
- 동일한 브라우저 창에서 다른 탭으로 변경하십시오.
- 웹 페이지의 스크린 샷 찍기
이 메소드를 사용하여 개발자는 자동 테스트를 검사 할 수 있습니다.
- 요소가 페이지에 있고 사용자가 볼 수있는 경우
- 검색 또는 로그인 양식
- 버튼 또는 상호 작용 요소
- 요소의 값 또는 속성 확인
Selenium은 일반적인 웹 브라우저와 비슷한 webdrivers에서 실행되지만 Selenium과 상호 작용할 수 있습니다. 셀레늄 테스트는 일반적으로 개발자가 테스트하고있는 브라우저의 새 드라이버 인스턴스를 열어줍니다. 이는 항상 깨끗한 슬레이트입니다. 이렇게하면 Selenium 테스트를 실행할 때 개발자가 이전 쿠키 또는 브라우저 캐시가 애플리케이션 결과에 영향을 미치지 않도록 할 수 있습니다.
셀레늄은 헤드리스 모드에서 웹 드라이버를 실행할 때도 작동합니다.
버전
번역 | 출시일 |
---|---|
3.4.0 | 2017-04-11 |
3.3 | 2017-04-07 |
3.2 | 2017-02-27 |
3.1 | 2017-02-13 |
3.0.1 | 2016-11-19 |
3.0 | 2016-10-11 |
자바에서 간단한 셀렌 테스트
아래 코드는 셀렌을 사용하는 간단한 자바 프로그램입니다. 아래 코드의 여정은 다음과 같습니다.
- Firefox 브라우저 열기
- Google 페이지 열기
- Google 페이지의 제목 인쇄
- 검색 창 위치 찾기
- 검색 창에 값을 Selenium으로 전달하십시오.
- 양식 제출
- 브라우저 종료
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time
// when trying to find an element or elements if they are not immediately available.
// The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Maximize the browser window to fit into screen
driver.manage().window().maximize();
// Visit Google
driver.get("http://www.google.com");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Selenium!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
//Close the browser
driver.quit();
}
}
Python으로 간단한 셀레늄 테스트
from selenium import webdriver
# Create a new chromedriver
driver = webdriver.Chrome()
# Go to www.google.com
driver.get("https://www.google.com")
# Get the webelement of the text input box
search_box = driver.find_element_by_name("q")
# Send the string "Selenium!" to the input box
seach_box.send_keys("Selenium!")
# Submit the input, which starts a search
search_box.submit()
# Wait to see the results of the search
time.sleep(5)
# Close the driver
driver.quit()
터미널 (BASH)을 통해 Python Selenium 설정하기
가장 쉬운 방법은 pip 와 VirtualEnv 를 사용하는 것입니다. Selenium은 또한 python 3이 필요합니다. * .
다음을 사용하여 virtualenv를 설치하십시오.
$: pip install virtualenv
Selenium 파일을위한 디렉토리를 생성하거나 입력하십시오 :
$: cd my_selenium_project
Selenium 파일의 디렉토리에 새 VirtualEnv를 만듭니다.
$: virtualenv -p /usr/bin/python3.0 venv
VirtualEnv 활성화 :
$: source venv/bin/active
이제 각 bash 라인의 시작 부분에 (venv)가 보일 것입니다. pip를 사용하여 Selenium 설치 :
$: pip install selenium
Selenium은 기본적으로 FireFox 드라이버와 함께 제공됩니다.
Google 크롬에서 Selenium을 실행하려면 다음과 같이하십시오.
$: pip install chromedriver
이제 버전 제어 VirtualEnv가 있습니다. 모든 것이 올바르게 설정되었는지 확인하려면 다음을 수행하십시오.
파이썬 시작 :
$: python
인쇄물 :
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
새 웹 드라이브 (이 경우 크롬 드라이브)를 만들고 www.google.com으로 이동하십시오.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.google.com")
드라이버와 파이썬 인터프리터를 닫습니다 :
>>> driver.quit()
>>> quit()
VirtualEnv 비활성화 :
$: deactivate
line driver = webdriver.Chrome()
오류가 발생하는 경우 :
- 크롬 브라우저가 설치되어 있는지 확인하십시오. 그렇게하지 않으면 Selenium 크롬 드라이버가 Chrome 바이너리에 액세스 할 수 없습니다.
- webdriver.Chrome ()은 chromedriver 위치에 대한 매개 변수를 사용할 수도 있습니다. pip를 사용하여 설치 한 경우 (mac에서)
driver = webdriver.Chrome("./venv/selenium/webdriver/chromedriver")
.
C #의 간단한 셀렌 예제
//Create a new ChromeDriver
IWebDriver driver = new ChromeDriver();
//Navigate to www.google.com
driver.Navigate().GoToUrl("https://www.google.com");
//Find the WebElement of the search bar
IWebElement element = driver.FindElement(By.Name("q"));
//Type Hello World into search bar
element.SendKeys("Hello World");
//Submit the input
element.Submit();
//Close the browser
driver.Quit();