标签:
之前的文章讲述了通过 WebDriver 如何启动 Firefox、Chrome 浏览器,及其相应的 MacOX 环境配置,请各位小主们参阅以下链接:
既然是在 MacOX 中穿插学习 PySe,那怎么可能少了 Safari 浏览器呢,幸运的是 Selenium2 支持了 Safari 浏览器,下面就开始我们的 Selenium2 Safari 之旅吧。
一、Safari 环境配置
安装 Safari 浏览器插件: SafariDriver.safariextz
首先,若想启动 Safari 浏览器,WebDriver 同样需要安装 Safari 的 driver,
安装好 Safari WebDriver 扩展插件后,如下图所示
另外,也可以通过 selenium-server-standalone-2.46.0.jar 中获取相应版本的 SafariDriver.safariextz
通过执行命令:jar xf selenium-server-standalone-2.46.0.jar 可解压 jar 文件,过程中出现的 license 目录无法创建可忽略。
配置环境变量:SELENIUM_SERVER_JAR
将 selenium-server-standalone-2.46.0.jar 放置在一个空文件夹,例如:cp selenium-server-standalone-2.46.0.jar /Library/Selenium/selenium-server-standalone-2.46.0.jar
然后通过以下命令,添加环境变量 SELENIUM_SERVER_JAR
export SELENIUM_SERVER_JAR="/Library/Selenium/selenium-server-standalone-2.46.0.jar"
若未配置环境变量,则在执行测试脚本时,会有如下所示的错误信息,提示添加环境变量
Aaron-test:pyse aaron$ python pyse_demo_safari.py Traceback (most recent call last): File "pyse_demo_safari.py", line 6, in <module> browser = webdriver.Safari() File "/Library/Python/2.7/site-packages/selenium/webdriver/safari/webdriver.py", line 54, in __init__ ‘SELENIUM_SERVER_JAR‘") Exception: No executable path given, please add one to Environment Variable ‘SELENIUM_SERVER_JAR‘ Aaron-test:pyse aaron$
二、编写示例脚本
启动 Safari 的自动化脚本的工作事项如下所示:
相应的脚本 pyse_demo_safari.py 内容如下所示:
1 #!/usr/local/bin/python 2 # -*- coding: utf-8 -*- 3 4 from selenium import webdriver 5 import time 6 7 # open safari 8 safari = webdriver.Safari() 9 10 safari.set_window_position(20, 100) 11 safari.set_window_size(1000, 700) 12 13 # navigate to baidu 14 safari.get("http://www.baidu.com/") 15 16 safari.implicitly_wait(4) 17 18 # get input key 19 txt_search_key = safari.find_element_by_id("kw") 20 21 # clear input and type key 22 txt_search_key.clear() 23 txt_search_key.send_keys(u"范丰平") 24 25 # get search and click 26 btn_search = safari.find_element_by_id("su") 27 btn_search.click() 28 29 safari.implicitly_wait(10) 30 31 # click link text 32 link = safari.find_element_by_partial_link_text(u"范丰平") 33 34 print link.text 35 36 time.sleep(5) 37 38 safari.close() 39 40 safari.quit()
三、执行脚本
进入命令行控制台,进入 pyse_demo_safari.py 所在目录,执行命令 python pyse_demo_safari.py,执行后出现了如下所示的错误,不知哪位大神可知原因,劳烦告知,非常感谢!
1 Aaron-test:pyse aaron$ python pyse_demo_safari.py 2 13:20:31.244 INFO - Launching a standalone Selenium Server 3 13:20:31.275 INFO - Java: Oracle Corporation 25.40-b25 4 13:20:31.275 INFO - OS: Mac OS X 10.10.2 x86_64 5 13:20:31.284 INFO - v2.46.0, with Core v2.46.0. Built from revision 87c69e2 6 13:20:31.359 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: 7 registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC 8 13:20:31.360 INFO - Driver class not found: com.opera.core.systems.OperaDriver 9 13:20:31.360 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered 10 13:20:31.457 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:55186/wd/hub 11 13:20:31.457 INFO - Selenium Server is up and running 12 13:20:41.025 INFO - Executing: [new session: Capabilities [{browserName=safari, javascriptEnabled=true, version=, platform=ANY}]]) 13 13:20:41.033 INFO - Creating a new session for Capabilities [{browserName=safari, javascriptEnabled=true, version=, platform=ANY}] 14 13:20:41.088 INFO - Server started on port 41255 15 13:20:41.095 INFO - Launching Safari 16 13:20:41.103 INFO - Waiting for SafariDriver to connect 17 13:20:43.819 INFO - Connection opened 18 13:20:43.838 INFO - Driver connected in 2735 ms 19 13:20:43.876 INFO - Shutting down 20 13:20:43.876 INFO - Closing connection 21 13:20:43.877 INFO - Stopping Safari 22 13:20:43.987 INFO - Stopping server 23 13:20:43.987 INFO - Stopping server 24 13:20:43.996 INFO - Shutdown complete 25 13:20:43.997 WARN - Exception thrown 26 java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException 27 Build info: version: ‘2.46.0‘, revision: ‘87c69e2‘, time: ‘2015-06-04 16:16:47‘ 28 System info: host: ‘aaron-test.local‘, ip: ‘127.0.0.1‘, os.name: ‘Mac OS X‘, os.arch: ‘x86_64‘, os.version: ‘10.10.2‘, java.version: ‘1.8.0_40‘ 29 Driver info: driver.version: unknown 30 at java.util.concurrent.FutureTask.report(FutureTask.java:122) 31 at java.util.concurrent.FutureTask.get(FutureTask.java:192) 32 at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:183) 33 at org.openqa.selenium.remote.server.DefaultSession.<init>(DefaultSession.java:119) 34 at org.openqa.selenium.remote.server.DefaultSession.createSession(DefaultSession.java:95) 35 at org.openqa.selenium.remote.server.DefaultDriverSessions.newSession(DefaultDriverSessions.java:137) 36 at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:58) 37 at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:1) 38 at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:111) 39 at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:174) 40 at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:202) 41 at org.openqa.selenium.remote.server.DriverServlet.doPost(DriverServlet.java:164) 42 at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 43 at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:130) 44 at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 45 at org.openqa.jetty.jetty.servlet.ServletHolder.handle(ServletHolder.java:428) 46 at org.openqa.jetty.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:680) 47 at org.openqa.jetty.jetty.servlet.ServletHandler.handle(ServletHandler.java:571) 48 at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1526) 49 at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1479) 50 at org.openqa.jetty.http.HttpServer.service(HttpServer.java:920) 51 at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820) 52 at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986) 53 at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837) 54 at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:243) 55 at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:358) 56 at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:537) 57 Caused by: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException 58 Build info: version: ‘2.46.0‘, revision: ‘87c69e2‘, time: ‘2015-06-04 16:16:47‘ 59 System info: host: ‘aaron-test.local‘, ip: ‘127.0.0.1‘, os.name: ‘Mac OS X‘, os.arch: ‘x86_64‘, os.version: ‘10.10.2‘, java.version: ‘1.8.0_40‘ 60 Driver info: driver.version: unknown 61 at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:95) 62 at org.openqa.selenium.remote.server.DefaultDriverProvider.newInstance(DefaultDriverProvider.java:79) 63 at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:62) 64 at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:222) 65 at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:1) 66 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 67 at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176) 68 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 69 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 70 at java.lang.Thread.run(Thread.java:745) 71 Caused by: java.lang.reflect.InvocationTargetException 72 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 73 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 74 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 75 at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 76 at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:85) 77 ... 9 more 78 Caused by: org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 79 Build info: version: ‘2.46.0‘, revision: ‘87c69e2‘, time: ‘2015-06-04 16:16:47‘ 80 System info: host: ‘aaron-test.local‘, ip: ‘127.0.0.1‘, os.name: ‘Mac OS X‘, os.arch: ‘x86_64‘, os.version: ‘10.10.2‘, java.version: ‘1.8.0_40‘ 81 Driver info: driver.version: SafariDriver 82 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) 83 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242) 84 at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128) 85 at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141) 86 at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:58) 87 at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:51) 88 ... 14 more 89 Caused by: java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to java.lang.String 90 at org.openqa.selenium.remote.JsonToBeanConverter.convert(JsonToBeanConverter.java:111) 91 at org.openqa.selenium.remote.JsonToBeanConverter.convert(JsonToBeanConverter.java:42) 92 at org.openqa.selenium.safari.SafariDriverCommandExecutor.execute(SafariDriverCommandExecutor.java:191) 93 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:578) 94 ... 19 more 95 13:20:44.001 WARN - Exception: com.google.gson.JsonObject cannot be cast to java.lang.String 96 Traceback (most recent call last): 97 File "pyse_demo_safari.py", line 8, in <module> 98 browser = webdriver.Safari() 99 File "/Library/Python/2.7/site-packages/selenium/webdriver/safari/webdriver.py", line 60, in __init__ 100 desired_capabilities=desired_capabilities) 101 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 87, in __init__ 102 self.start_session(desired_capabilities, browser_profile) 103 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 136, in start_session 104 ‘desiredCapabilities‘: desired_capabilities, 105 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 196, in execute 106 self.error_handler.check_response(response) 107 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response 108 raise exception_class(message, screen, stacktrace) 109 selenium.common.exceptions.WebDriverException: Message: com.google.gson.JsonObject cannot be cast to java.lang.String 110 Stacktrace: 111 at org.openqa.selenium.remote.JsonToBeanConverter.convert (JsonToBeanConverter.java:111) 112 at org.openqa.selenium.remote.JsonToBeanConverter.convert (JsonToBeanConverter.java:42) 113 at org.openqa.selenium.safari.SafariDriverCommandExecutor.execute (SafariDriverCommandExecutor.java:191) 114 at org.openqa.selenium.remote.RemoteWebDriver.execute (RemoteWebDriver.java:578) 115 at org.openqa.selenium.remote.RemoteWebDriver.startSession (RemoteWebDriver.java:242) 116 at org.openqa.selenium.remote.RemoteWebDriver.<init> (RemoteWebDriver.java:128) 117 at org.openqa.selenium.remote.RemoteWebDriver.<init> (RemoteWebDriver.java:141) 118 at org.openqa.selenium.safari.SafariDriver.<init> (SafariDriver.java:58) 119 at org.openqa.selenium.safari.SafariDriver.<init> (SafariDriver.java:51) 120 at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (NativeConstructorAccessorImpl.java:-2) 121 at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62) 122 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45) 123 at java.lang.reflect.Constructor.newInstance (Constructor.java:422) 124 at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor (DefaultDriverProvider.java:85) 125 at org.openqa.selenium.remote.server.DefaultDriverProvider.newInstance (DefaultDriverProvider.java:79) 126 at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance (DefaultDriverFactory.java:62) 127 at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:222) 128 at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:1) 129 at java.util.concurrent.FutureTask.run (FutureTask.java:266) 130 at org.openqa.selenium.remote.server.DefaultSession$1.run (DefaultSession.java:176) 131 at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) 132 at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617) 133 at java.lang.Thread.run (Thread.java:745)
对应的 Safari 浏览器版本如下所示:
解决办法:
1、更新 Safari 的插件 SafariDriver.safariextz 为 selenium-server-standalone-2.47.1.jar 中的 SafariDriver.safariextz
2、更新 selenium-server-standlone-2.46.0.jar 为 selenium-server-standalone-2.47.1.jar
3、更新 SELENIUM_SERVER_JAR 变量为 : /Library/Selenium/selenium-server-standalone-2.47.1.jar
4、重新执行脚本 python pyse_demo_safari.py,执行日志如下所示:
16:18:56.330 INFO - Launching a standalone Selenium Server 16:18:56.360 INFO - Java: Oracle Corporation 25.40-b25 16:18:56.361 INFO - OS: Mac OS X 10.10.2 x86_64 16:18:56.370 INFO - v2.47.1, with Core v2.47.1. Built from revision 411b314 16:18:56.429 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC 16:18:56.429 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped: registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC 16:18:56.429 INFO - Driver class not found: com.opera.core.systems.OperaDriver 16:18:56.429 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered 16:18:56.488 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:57241/wd/hub 16:18:56.488 INFO - Selenium Server is up and running 16:19:06.330 INFO - Executing: [new session: Capabilities [{browserName=safari, javascriptEnabled=true, version=, platform=ANY}]]) 16:19:06.338 INFO - Creating a new session for Capabilities [{browserName=safari, javascriptEnabled=true, version=, platform=ANY}] 16:19:06.394 INFO - Server started on port 39043 16:19:06.402 INFO - Launching Safari 16:19:06.410 INFO - Waiting for SafariDriver to connect 16:19:07.137 INFO - Connection opened 16:19:07.138 INFO - Driver connected in 728 ms 16:19:07.183 INFO - Done: [new session: Capabilities [{browserName=safari, javascriptEnabled=true, version=, platform=ANY}]] 16:19:07.195 INFO - Executing: [set window position]) 16:19:07.211 INFO - Done: [set window position] 16:19:07.215 INFO - Executing: [set window size]) 16:19:07.744 INFO - Done: [set window size] 16:19:07.748 INFO - Executing: [get: http://www.baidu.com/]) 16:19:09.139 INFO - Done: [get: http://www.baidu.com/] 16:19:09.144 INFO - Executing: [implicitly wait: 4000]) 16:19:09.150 INFO - Done: [implicitly wait: 4000] 16:19:09.155 INFO - Executing: [find element: By.id: kw]) 16:19:09.252 INFO - Done: [find element: By.id: kw] 16:19:09.255 INFO - Executing: [clear: 0 [[SafariDriver: safari on MAC (null)] -> id: kw]]) 16:19:09.261 INFO - Done: [clear: 0 [[SafariDriver: safari on MAC (null)] -> id: kw]] 16:19:09.265 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> id: kw], [范, 丰, 平]]) 16:19:09.332 INFO - Done: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> id: kw], [范, 丰, 平]] 16:19:09.335 INFO - Executing: [find element: By.id: su]) 16:19:09.339 INFO - Done: [find element: By.id: su] 16:19:09.342 INFO - Executing: [click: 1 [[SafariDriver: safari on MAC (null)] -> id: su]]) 16:19:09.361 INFO - Done: [click: 1 [[SafariDriver: safari on MAC (null)] -> id: su]] 16:19:09.364 INFO - Executing: [implicitly wait: 10000]) 16:19:09.384 INFO - Done: [implicitly wait: 10000] 16:19:09.390 INFO - Executing: [find element: By.partialLinkText: 范丰平]) 16:19:09.843 INFO - Done: [find element: By.partialLinkText: 范丰平] 16:19:09.846 INFO - Executing: [get text: 2 [[SafariDriver: safari on MAC (null)] -> partial link text: 范丰平]]) 16:19:09.851 INFO - Done: [get text: 2 [[SafariDriver: safari on MAC (null)] -> partial link text: 范丰平]] 范丰平 - 博客园 16:19:14.855 INFO - Executing: [close window]) 16:19:14.864 INFO - Done: [close window] 16:19:14.867 INFO - Executing: [delete session: 5109aef4-10ad-4602-91e7-17be071473a8]) 16:19:14.867 INFO - Shutting down 16:19:14.867 INFO - Closing connection 16:19:14.868 INFO - Stopping Safari 16:19:14.924 INFO - Stopping server 16:19:14.924 INFO - Stopping server 16:19:14.929 INFO - Shutdown complete 16:19:14.929 INFO - Done: [delete session: 5109aef4-10ad-4602-91e7-17be071473a8]
执行结果如图所示:
至此, PySe-006-Se-WebDriver 启动浏览器之三 - Safari 顺利完结,希望此文能够给初学 Python + Selenium2 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
PySe-006-Se-WebDriver 启动浏览器之三 - Safari
标签:
原文地址:http://www.cnblogs.com/fengpingfan/p/4709644.html