Question

I'm trying to run the following python script:

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')

However, every time I try to run it, I get this error:

    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$ 

But I've installed the module using pip install selenium and it installed fine. When I run a new terminal window, enter python, and then type in from selenium import webdriver, it imports fine. If I exit() python, and then re-enter and try again, the same above error happens, in that it can't import selenium. If I re-open terminal, then it works again, but only in the terminal python window. I can even type out every line of code and it prints the images fine in the terminal!

It never works if I just try to run the script on its own. Any ideas as to why this is? Thanks!!!

Was it helpful?

Solution

WOW. I can't believe this, but my little script, which I so simply named "selenium.py", was the problem. The answer is, DON'T DO THIS! When the script said from selenium import webdriver, it somehow thought it was calling itself, and creating major errors.

I renamed the script to "myselenium.py" and it worked fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top