问题 01 :selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.
原因:代码中使用的浏览器是Chrome,异常提示在Path中找不到 ChromeDriver
解决方法:下载ChromeDriver.exe文件,并把它放到Path下(因为我在环境变量Path中设置了Python目录C:\Python27,所以我放在C:\Python27下,程序就可以正常执行了)
问题 02:TypeError: 'unicode' object is not callabl
原因 :把属性当做方法调用了,比如:driver.title 写成了 dirver.title()
解决方法:把()去掉
问题03:按钮是一个Span,不能直接点击。例如 <span>Add to cart</span>
解决方法:可以用Action(http://stackoverflow.com/questions/19897183/python-selenium-how-to-click-on-a-javascript-button)
from selenium.webdriver.common.action_chains import ActionChains
self.driver = webdriver.Firefox()
# You need a mouse to hover the span elements here
self.mouse = webdriver.ActionChains(self.driver)
# You need get the span element from its xpath:
value = 'Add to cart'
span_xpath = '//span[contains(text(), "' + value + '")]'
span_element = driver.find_element_by_xpath(span_xpath)
# Then you hover on span element by mouse and click on it:
self.mouse.move_to_element(span_element).click().perform()
问题04:#!/usr/bin/env python 会去环境设置寻找python目录,推荐这种写法