Selenium은 웹 애플리케이션 테스트를 위한 도구입니다. Selenium 테스트는 실제 사용자와 마찬가지로 브라우저에서 직접 실행됩니다. "Web Automated Testing (2) "의 마지막 장에서는 주로 웹 테스트에서 Selenium 3가 사용하는 문제 세트와 솔루션에 대해 이야기했습니다. 이 문서에서는 주로 참조용으로 IE, Firefox 및 Chrome을 시작하기 위한 코드 예제에 대해 설명합니다.
IE, Firefox, Chrome을 시작하기 전에 해당 브라우저의 드라이버 서버를 Windows 시스템 경로 디렉터리로 설정해야 합니다.
예를 들어, 내 드라이버는 C:Program Files (x86)seleniumdriver 디렉터리에 있습니다. 다음은 Windows 시스템 경로 경로 설정입니다.
IE 코드 시작:
#!/usr/bin/env python #coding=utf-8 from selenium import webdriver import os from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary driver = webdriver.Ie() # go to the google home page driver.get("https://www.baidu.com/") # the page is ajaxy so the title is originally this: print driver.title # find the element that's name attribute is q (the google search box) inputElement = driver.find_element_by_name("wd") # type in the search inputElement.send_keys("cheese!") # submit the form (although google automatically searches now without submitting) inputElement.submit() try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait(driver, 10).until(EC.title_contains("cheese!")) # You should see "cheese! - Google Search" print driver.title finally: pass #driver.quit()
FireFox 코드 시작: # 🎜🎜#
#!/usr/bin/env python #coding=utf-8 from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary # Create a new instance of the Firefox driver #binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') #driver = webdriver.Firefox(firefox_binary=binary) driver = webdriver.Firefox() # go to the google home page driver.get("https://www.baidu.com/") # the page is ajaxy so the title is originally this: print driver.title # find the element that's name attribute is q (the google search box) inputElement = driver.find_element_by_name("wd") # type in the search inputElement.send_keys("cheese!") # submit the form (although google automatically searches now without submitting) inputElement.submit() try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait(driver, 10).until(EC.title_contains("cheese!")) # You should see "cheese! - Google Search" print driver.title finally: pass #driver.quit()
Chrome 코드 시작:
#!/usr/bin/env python #coding=utf-8 from selenium import webdriver import os from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary driver = webdriver.Chrome() # go to the google home page driver.get("https://www.baidu.com/") # the page is ajaxy so the title is originally this: print driver.title # find the element that's name attribute is q (the google search box) inputElement = driver.find_element_by_name("wd") # type in the search inputElement.send_keys("cheese!!") # submit the form (although google automatically searches now without submitting) inputElement.submit() try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait(driver, 10).until(EC.title_contains("cheese!")) # You should see "cheese! - Google Search" print driver.title finally: pass #driver.quit()[추천 과정:
Python 비디오 튜토리얼】
위 내용은 웹 자동화 테스트(2) Selenium 3은 IE, Firefox, Chrome 코드 예제를 시작합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!