查看python环境
C:\WINDOWS\system32>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
安装selenium
C:\WINDOWS\system32>pip install -U selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
100% |████████████████████████████████| 911kB 106kB/s
Collecting urllib3 (from selenium)
Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
100% |████████████████████████████████| 122kB 81kB/s
Installing collected packages: urllib3, selenium
Found existing installation: urllib3 1.21.1
Uninstalling urllib3-1.21.1:
Successfully uninstalled urllib3-1.21.1
Successfully installed selenium-3.141.0 urllib3-1.24.1
You are using pip version 9.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
在pycharm中运行第一个selenium程序
__author__ = 'fhzheng'
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = dirver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have produc names displayed
# currently on result page using find_element_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
# get the number of anchor elements found
print("Found " + str(len(products))+" products:")
# iterate through each anchor element and print the text that is
# name of the product
for product in products:
print(product.text)
# close the browser window
driver.quit()
运行出错
C:\Python\Python35-32\python.exe C:/Users/fhzheng/PycharmProjects/setests/searchproducts.py
Traceback (most recent call last):
File "C:\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Python\Python35-32\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35-32\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/fhzheng/PycharmProjects/setests/searchproducts.py", line 7, in <module>
driver = webdriver.Firefox()
File "C:\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Process finished with exit code 1
修改代码如下,完成测试
__author__ = 'fhzheng'
from selenium import webdriver
# create a new Firefox session
# F:\常用工具软件\firefoxdriver
# driver = webdriver.Firefox("C:/Program Files/Mozilla Firefox/geckodriver.exe")
# driver = webdriver.Firefox()
driver = webdriver.Chrome("F:/常用工具软件/chromedriver/chromedriver.exe") #正确的路径 使用反斜杠
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have produc names displayed
# currently on result page using find_element_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
# get the number of anchor elements found
print("Found " + str(len(products))+" products:")
# iterate through each anchor element and print the text that is
# name of the product
for product in products:
print(product.text)
# close the browser window
driver.quit()