1

我正在尝试运行以下 python 脚本:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('https://www.everlane.com/collections/mens-luxury-tees/products/mens-v-antique')

driver.save_screenshot('screen.png') # save a screenshot to disk
print driver.current_url

images = driver.find_elements_by_tag_name('img')
for image in images:
    print image.get_attribute('src')

但是,每次我尝试运行它时,都会收到此错误:

    FitValet-MacBook-Pro:desktop fitvalet$ python selenium.py
Traceback (most recent call last):
  File "selenium.py", line 1, in <module>
    from selenium import webdriver
  File "/Users/fitvalet/Desktop/selenium.py", line 1, in <module>
    from selenium import webdriver
ImportError: cannot import name webdriver
FitValet-MacBook-Pro:desktop fitvalet$ 

但是我已经安装了模块pip install selenium,并且安装得很好。当我运行一个新的终端窗口时,输入python,然后输入from selenium import webdriver,它可以正常导入。如果我exit()python,然后重新输入并重试,就会发生上述相同的错误,因为它无法导入selenium。如果我重新打开终端,它会再次工作,但仅在终端 python 窗口中。我什至可以输入每一行代码,它会在终端中很好地打印图像!

如果我只是尝试自己运行脚本,它永远不会起作用。关于为什么会这样的任何想法?谢谢!!!

4

1 回答 1

3

哇。我不敢相信,但我的小脚本(我简单地命名为“selenium.py”)是问题所在。答案是,不要这样做!当脚本说from selenium import webdriver时,它以某种方式认为它在调用自己,并产生重大错误。

我将脚本重命名为“myselenium.py”,它运行良好。

于 2013-09-29T01:35:00.563 回答