1 什么是selenium
????????????Selenium是一個Web的自動化測試工具普泡,最初是為網(wǎng)站自動化測試而開發(fā)的,Selenium 可以直接運行在瀏覽器上儒恋,它支持所有主流的瀏覽器(包括PhantomJS這些無界面的瀏覽器)喇澡,可以接收指令,讓瀏覽器自動加載頁面小泉,獲取需要的數(shù)據(jù)勒叠,甚至頁面截屏。
2. 在windows10? ?python3.7? pycharm 中運行代碼
from seleniumimport webdriver
driver= webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
# driver = webdriver.Chrome(r"D:\chromedriver\chromedriver.exe")
driver.get('http://www.baidu.com')
driver.find_element_by_id('kw').send_keys('haha')
driver.find_element_by_id('su').click()
直接報錯:
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe C:/Users/Administrator/Desktop/爬蟲/demo/test2.py
Traceback (most recent call last):
? File "C:/Users/Administrator/Desktop/爬蟲/demo/test2.py", line 4, in <module>
? ? driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
? File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
? ? self.service.start()
? File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
? ? self.assert_process_still_running()
? File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 111, in assert_process_still_running
? ? % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0
上面加粗部分是主要報錯內(nèi)容膏孟。
主要原因是在創(chuàng)建driver對象的時候要傳入?yún)?shù),參數(shù)傳遞錯誤拌汇。
修改后的代碼:
? ??driver = webdriver.Chrome(r"D:\chromedriver\chromedriver.exe")
就可以了柒桑。