问题
I'm trying to use Safari WebDriver, and I followed the instructions here to build a Safari Driver, but now I don't know how to import it and use it in my code.
I'm using python on Mac OSX
回答1:
Using Python 2.7.5 and python module selenium (2.41.0).
This example opens a Safari browser and does my bidding:
# -*- coding: utf-8 -*-
print '''
Python Selenium Safari Example
'''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
# path to selenium server standalone jar, downloaded here:
# http://docs.seleniumhq.org/download/
# or a direct url:
# http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar
os.environ["SELENIUM_SERVER_JAR"] = "selenium-server-standalone-2.41.0.jar"
# note: I've put this jar file in the same folder as this python file
browser = webdriver.Safari()
# makes the browser wait if it can't find an element
browser.implicitly_wait(10)
browser.get("http://google.com/")
search_input = browser.find_element_by_css_selector("#gbqfq")
search_input.send_keys("python SELENIUM_SERVER_JAR turn logging off")
search_input.send_keys(Keys.RETURN)
raw_input("Press Enter to close...")
browser.quit()
but here is what appears in the terminal when I run it:
$ python selenium_safari_example.py
Python Selenium Safari Example
May 27, 2014 4:24:17 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
16:24:17.918 INFO - Java: Apple Inc. 20.65-b04-462
16:24:17.918 INFO - OS: Mac OS X 10.7.5 x86_64
16:24:17.975 INFO - v2.41.0, with Core v2.41.0. Built from revision 3192d8a
16:24:18.418 INFO - Default driver org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: registration capabilities Capabilities [{platform=WINDOWS, ensureCleanSession=true, browserName=internet explorer, version=}] does not match with current platform: MAC
16:24:18.597 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:61893/wd/hub
16:24:18.598 INFO - Version Jetty/5.1.x
16:24:18.599 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
16:24:18.600 INFO - Started HttpContext[/selenium-server,/selenium-server]
16:24:18.600 INFO - Started HttpContext[/,/]
16:24:18.724 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@75e845c2
16:24:18.724 INFO - Started HttpContext[/wd,/wd]
16:24:18.732 INFO - Started SocketListener on 0.0.0.0:61893
16:24:18.732 INFO - Started org.openqa.jetty.jetty.Server@1ac88440
16:24:27.335 INFO - Executing: [new session: Capabilities [{platform=ANY, javascriptEnabled=true, browserName=safari, version=}]] at URL: /session)
16:24:27.351 INFO - Creating a new session for Capabilities [{platform=ANY, javascriptEnabled=true, browserName=safari, version=}]
16:24:27.580 INFO - Server started on port 1988
16:24:27.772 INFO - Launching Safari
16:24:27.928 INFO - Waiting for SafariDriver to connect
16:24:39.589 INFO - Connection opened
16:24:39.610 INFO - Driver connected in 11681 ms
16:24:39.813 INFO - Done: /session
16:24:39.917 INFO - Executing: [implicitly wait: 10000] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/timeouts/ implicit_wait)
16:24:39.962 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/timeouts/implicit_wait
16:24:39.967 INFO - Executing: [get: http://google.com/] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/url)
16:24:47.853 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/url
16:24:47.860 INFO - Executing: [find element: By.selector: #gbqfq] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/ element)
16:24:48.372 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element
16:24:48.382 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> css selector: #gbqfq], [p, y, t, h, o, n, , S, E, L, E, N, I, U, M, _, S, E, R, V, E, R, _, J, A, R, , t, u, r, n, , l, o, g, g, i, n, g, , o, f, f]] at URL: / session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value)
16:24:48.537 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value
Press Enter to close...
16:24:48.543 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> css selector: #gbqfq], [?]] at URL: / session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value)
16:24:49.113 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value
16:24:59.122 INFO - Executing: [delete session: 24ca27ce-7f06-4d16-ab8f-3d7376b01eea] at URL: /session/24ca27ce-7f06-4d16- ab8f-3d7376b01eea)
16:24:59.123 INFO - Shutting down
16:24:59.123 INFO - Closing connection
16:24:59.124 INFO - Stopping Safari
16:24:59.333 INFO - Stopping server
16:24:59.333 INFO - Stopping server
16:24:59.382 INFO - Uninstalling extensions
16:24:59.383 INFO - Shutdown complete
16:24:59.385 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea
$
If someone knows if you're able to disable logging from the jar file through a python call I'd love to know how.
回答2:
If you are using safari version 12 and later and Mac version High Sierra and later just make sure Safari’s executable is located at /usr/bin/safaridriver
and
Run once safaridriver --enable
in terminal and start running selenium scripts os safari
来源:https://stackoverflow.com/questions/20934464/how-to-use-selenium-safari-webdriver