| Home: | http://www.seleniumhq.org |
| Docs: | selenium package API |
| Dev: | https://github.com/SeleniumHQ/Selenium |
| PyPI: | https://pypi.org/project/selenium/ |
| IRC: | #selenium channel on freenode |
Immer wieder kommt aber die Frage auf: „Wo muss der geckodriver oder die Webdriver liegen?“
Einfache Antwort “ Vollkommen egal wohin ihr ihn legt! “
Woher bekommt man die Treiber?
Integration des Chrome Drivers in Python
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
Weitere Informationen zu diesem Thema findet man hier: http://chromedriver.chromium.org/getting-started
Die Lebensdauer des Chrome Driver steuern
Die ChromeDriver-class startet den ChromeDriver-Serverprozess bei der Erstellung und beendet ihn, wenn das Beenden aufgerufen wird. Dies kann bei großen Testsuiten, bei denen pro Test eine ChromeDriver-Instanz erstellt wird, eine erhebliche Zeitverschwendung bedeuten. Es gibt zwei Möglichkeiten, dies zu beheben:
1. Verwendet den ChromeDriverService. Dieser ist für die meisten Sprachen verfügbar und ermöglicht es euch, den ChromeDriver-Server selbst zu starten / stoppen.
Hier ein Beispiel für Python:
import time
from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('/path/to/chromedriver')
service.start()
capabilities = {'chrome.binary': '/path/to/custom/chrome'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
driver.quit()
Woher bekomme ich weitere Treiber:
Für Opera bekommt ihr bei github einen passenden Treiber. Wie ihr diesen
Opera: https://github.com/operasoftware/operachromiumdriver
Wie man den Opera Webdriver im Android Bereich einbindet könnt ihr hier in diesem Beispiel einsehen:
Opera und die android.py
import time
from selenium import webdriver
from selenium.webdriver.chrome import service
webdriver_service = service.Service('path/to/operadriver')
webdriver_service.start()
android_caps = {}
android_caps['operaOptions'] = {
'androidPackage': 'com.opera.browser',
}
driver = webdriver.Remote(webdriver_service.service_url, desired_capabilities=android_caps) #Android
driver.get('https://www.google.com/')
input_txt = driver.find_element_by_name('q')
input_txt.send_keys('operadriver\n')
time.sleep(5) #see the result
driver.quit()
Wie man den Opera Driver in Appium einbindet könnt ihr hier sehen:
appium
import os
import time
from appium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ExpectedConditions
######## WEB DRIVER INITIALIZATION #############################
desired_caps = {}
desired_caps['automationName'] = 'selendroid'
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = ''
desired_caps['chromedriverExecutable'] = 'path/to/operadriver' #download from https://github.com/operasoftware/operachromiumdriver/releases
desired_caps['app'] = os.path.abspath('path/to/opera-browser.apk') #download it from http://www.opera.com/mobile/operabrowser/android
desired_caps['appPackage'] = 'com.opera.browser'
desired_caps['androidDeviceSocket'] = desired_caps['appPackage'] + '.devtools'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
######## UTILITIES #############################
def swipe_left(duration=1):
WebDriverWait(driver, 10).until(
ExpectedConditions.presence_of_element_located((By.ID, get_element_id("drag_area"))))
pager = find_element_by_id('drag_area');
flick = TouchActions(driver).flick_element(pager, -1000, 0, 70);
flick.perform();
def get_element_id(el_id):
if desired_caps['automationName'].lower() == 'android':
el_id = "com.opera.android.browser:id/" + el_id
return el_id
def find_element_by_id(el_id):
el_id = get_element_id(el_id)
return driver.find_element_by_id(el_id)
######## TEST #############################
def skip_introduction_guide():
WebDriverWait(driver, 10).until(
ExpectedConditions.visibility_of_element_located((By.ID, "continue_button"))).click()
swipe_left()
swipe_left()
swipe_left()
WebDriverWait(driver, 20).until(ExpectedConditions.element_to_be_clickable((By.ID, "guide_finish_button"))).click()
def open_page_in_native_context(url):
url_field = WebDriverWait(driver, 20).until(ExpectedConditions.element_to_be_clickable((By.ID, get_element_id("url_field"))))
url_field.clear()
url_field.send_keys(url + "\n")
def close_native_dialog():
WebDriverWait(driver, 10).until(ExpectedConditions.element_to_be_clickable((By.ID, get_element_id("opera_dialog_button_negative")))).click()
skip_introduction_guide()
open_page_in_native_context("https://www.google.com/")
print "*** VIEW HANDLES: ***"
for h in driver.contexts:
print h
if not 'CHROMIUM' in driver.contexts:
print '\'CHROMIUM\' not in driver.contexts!'
driver.quit()
exit(1)
driver.switch_to.context('CHROMIUM')
driver.get("https://www.google.com/")
text_input = WebDriverWait(driver, 10).until(ExpectedConditions.element_to_be_clickable((By.NAME, "q")))
text_input.send_keys('OperaDriver\n')
driver.switch_to.context('NATIVE_APP')
close_native_dialog()
time.sleep(5) #just to see the dialog is closed
driver.quit()
Wohin installieren?
Grundsätzlich kann man die Treiber dahin legen wohin man will. Wir binden diese entsprechend über Python ein, somit auf den Ordner und das File einzeln.
Wie binde ich den Treiber in den Source ein?
Chrome
from selenium import webdriver
url="C:\\Programs\\Python36\\BrowersDriver\\chromedriver.exe"
driver=webdriver.Chrome(url)
driver.get("http://www.yahoo.com")
driver.close()
driver.quit()
Neueste Kommentare