fedora23 安装使用web的自动化测试工具selenium
1、pip install selenium 需要root权限
2、运行一个实例 openchrome.py :
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time browser = webdriver.Chrome() # Get local session of Chrome browser.get("http://www.bing.com") # Load page assert "Bing" in browser.title elem = browser.find_element_by_name("q") # Find the query box elem.send_keys("seleniumhq" + Keys.RETURN) time.sleep(0.2) # Let the page load, will be added to the API try: browser.find_element_by_xpath("//a[contains(@href,'http://docs.seleniumhq.org/')]") except NoSuchElementException: assert 0, "can't find seleniumhq" browser.close()
报错:
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
使用提示的链接下载chromedriver,下载好解压即可
将chromedriver所在路径加到PATH变量里:vim ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/myname/.browserdriver/chromedriver export PATH
把 /home/myname/.browserdriver/chromedriver 追加到 PATH 后面
source ~/.bash_profile让路径生效
再次执行 python openchrome.py ,报错:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome version must be >= 55.0.2883.0
(Driver info: chromedriver=2.28.455506 (18f6627e265f442aeec9b6661a49fe819aeeea1f),platform=Linux 4.2.3-300.fc23.x86_64 x86_64)
原因是下载的chromedirver是最新版,chrome版本较旧 ,更新chrome:dnf update google-chrome
$sudo dnf update google-chrome RPM Fusion for Fedora 23 - Free - Updates 112 kB/s | 371 kB 00:03 Last metadata expiration check performed 0:00:00 ago on Sun Mar 26 17:01:21 2017. Dependencies resolved. ================================================================================================ Package Arch Version Repository Size ================================================================================================ Upgrading: google-chrome-stable x86_64 57.0.2987.110-1 google-chrome-unstable 45 M Transaction Summary ================================================================================================ Upgrade 1 Package Total download size: 45 M Is this ok [y/N]: y
google-chrome 需要配置 dnf 的 chrome 所用的源repository,这里默认从 rpmfusion free 里下了。
注:firefox会提示geckodriver
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "openfirefox.py", line 6, in <module> browser = webdriver.Firefox() # Get local session of firefox File "/usr/lib/python3.4/site-packages/selenium/webdriver/firefox/webdriver.py", line 145, in __init__ self.service.start() File "/usr/lib/python3.4/site-packages/selenium/webdriver/common/service.py", line 81, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
[1] http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html
[2] https://github.com/mozilla/geckodriver/releases
[3] http://stackoverflow.com/questions/40208051/selenium-using-python-geckodriver-executable-needs-to-be-in-path
[4] https://chromedriver.storage.googleapis.com/index.html