Python 2.7+selenium+Firefox 55.0.3
代码:
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time browser = webdriver.Firefox() # Get local session of firefox browser.get("http://www.yahoo.com") # Load page assert "Yahoo!" in browser.title elem = browser.find_element_by_name("p") # 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://seleniumhq.org')]") except NoSuchElementException: assert 0, "can't find seleniumhq" browser.close()
错误信息如下:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
回答摘自知乎:https://www.zhihu.com/question/49568096
1. selenium 3.x开始,webdriver/firefox/webdriver.py的__init__中,executable_path="geckodriver";而2.x是executable_path="wires"
2. firefox 47以上版本,需要下载第三方driver,即geckodriver;在http://docs.seleniumhq.org/download/的Third Party Drivers, Bindings, and Plugins下面找到Mozilla GeckoDriver,下载到任意电脑任意目录(Mac 下放到了/usr/bin/ 和/sbin/, /bin/,/usr/local/bin/),解压后将该路径加入到PC的path(针对windows)即可。
2. firefox 47以上版本,需要下载第三方driver,即geckodriver;在http://docs.seleniumhq.org/download/的Third Party Drivers, Bindings, and Plugins下面找到Mozilla GeckoDriver,下载到任意电脑任意目录(Mac 下放到了/usr/bin/ 和/sbin/, /bin/,/usr/local/bin/),解压后将该路径加入到PC的path(针对windows)即可。
作者:iceblue iceblue
链接:https://www.zhihu.com/question/49568096/answer/119324584
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。