1.測試實例:
# -*- coding: UTF-8 -*-
import unittest
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from utils.configfix import Config
from utils.logs import Logger
class Test_Baidu(unittest.TestCase):
URL = Config().getconfig('Config','URL',)
locator_kw = (By.ID, 'kw')
locator_su = (By.ID, 'su')
locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')
def setUp(self):
self.driver = webdriver.Firefox(log_path=r"E:\pythonproject\PythonSeleniumPrj\PythonSeleniumPrj\foxwatch.log")
self.driver.get(self.URL)
def tearDown(self):
self.driver.quit()
def test_search_0(self):
try:
Logger().logger.info("this is test!")
Logger().logger.info("this is just a test!")
#self.driver.find_element(*self.locator_kw).send_keys('pyth')
#self.driver.find_element(*self.locator_su).click()
#time.sleep(2)
#links = self.driver.find_elements(*self.locator_result)
#for link in links:
#print link.text
#Logger().logger.debug(link.text)
except:
Logger().logger.exception('exception logged: ')
if __name__ == '__main__':
unittest.main(verbosity=2)
環境是Python3.6.4+selenium3.9.0+Firefox58.0.1.6602
運行Python腳本時,提示以下錯誤:
Traceback (most recent call last):
File "E:\pythonproject\PythonSeleniumPrj\PythonSeleniumPrj\PythonSeleniumPrj.p
y", line 5, in <module>
driver = webdriver.Firefox()
File "D:\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py",
line 151, in __init__
log_path=log_path)
File "D:\Python36\lib\site-packages\selenium\webdriver\firefox\service.py", li
ne 44, in __init__
log_file = open(log_path, "a+") if log_path is not None and log_path != "" e
lse None
PermissionError: [Errno 13] Permission denied: 'geckodriver.log'
解決方法:重新定向日誌地址
def setUp(self):
self.driver = webdriver.Firefox(log_path=r"E:\pythonproject\PythonSeleniumPrj\PythonSeleniumPrj\foxwatch.log")
self.driver.get(self.URL)
2. Python 3 ImportError: No module named 'ConfigParser'
解決:在Python3裏ConfigParser改爲了configparser