背景
-
一個(gè)TX反饋運(yùn)行如下代碼
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get("https://cn.bing.com") driver.find_element("id","sb_form_q").send_keys("松勤軟件測(cè)試\n") sleep(3) driver.quit()
-
報(bào)錯(cuò)了
-
一看這個(gè)錯(cuò)誤沒(méi)見(jiàn)到過(guò),驅(qū)動(dòng)應(yīng)該是有的,版本也應(yīng)該對(duì)的,無(wú)法連接到chromedriver
-
考慮到chromedriver本身就是一個(gè)web server
C:\Users\songqin008>chromedriver Starting ChromeDriver 103.0.5060.134 (8ec6fce403b3feb0869b0732eda8bd95011d333c-refs/branch-heads/5060@{#1262}) on port 9515 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully.
-
其實(shí)你是可以在瀏覽器中訪問(wèn)http://127.0.0.1:9515/的,當(dāng)然這個(gè)get并不會(huì)返回太多有用的信息,只能說(shuō)可以看到。
解決
-
一開(kāi)始我只想到這里,但不知道如何解決
-
搜索引擎一查,說(shuō)是hosts文件可能會(huì)影響
-
查了下這位同學(xué)的hosts
-
少了下面一行
127.0.0.1 localhost
-
加上后解決
思考
-
猜猜:代碼自動(dòng)調(diào)起chromedriver,并作為webserver訪問(wèn)其api,這個(gè)地址可能是127.0.0.1?但沒(méi)有找到所以報(bào)錯(cuò)。
-
搜索代碼發(fā)現(xiàn),報(bào)錯(cuò)信息在selenium\webdriver\common\service.py,105行
while True: self.assert_process_still_running() if self.is_connectable(): break count += 1 sleep(0.5) if count == 60: raise WebDriverException("Can not connect to the Service %s" % self.path)
-
30秒超時(shí)時(shí)間會(huì)提示這個(gè),跟實(shí)際代碼運(yùn)行效果類似。
-
那代碼self.is_connectable()就應(yīng)該是不為True的
def is_connectable(self): return utils.is_connectable(self.port)
-
再看is的定義
def is_connectable(port: int, host: Optional[str] = "localhost") -> bool: pass #略
-
答案呼之欲出,上面的代碼默認(rèn)值就是localhost文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-817139.html
-
你現(xiàn)在沒(méi)有這個(gè),自然就不行咯文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-817139.html
到了這里,關(guān)于selenium中can not connect to the service chromedriver問(wèn)題的處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!