1

Right now I have the test code from the Selenium library. I am trying to test if it's gonna work and then do my own edits on it. However, it goes wrong on the 'import' part. I have all libraries installed already. Please help.

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Chrome()

# go to the google home page
driver.get("http://www.google.com")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

Exception is as followed:

Traceback (most recent call last):
  File "/Users/Edison/Desktop/supreme/supTest.py", line 12, in <module>
    from selenium import webdriver`enter code here`
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 32, in <module>
    from .extension_connection import ExtensionConnection
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 24, in <module>
    from selenium.webdriver.remote.remote_connection import RemoteConnection
  File "/Users/Edison/anaconda2/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 29, in <module>
    import urllib2 as url_request
  File "/Users/Edison/Desktop/supreme/urllib2.py", line 2, in <module>
    response = urllib2.urlopen('http://www.baidu.com/')
AttributeError: 'module' object has no attribute 'urlopen'
[Finished in 0.1s with exit code 1]
1
  • I tried the same code with Python-3.5, Selenium-3.0.1, Chrome-56, Chromedriver- and working fine. Please specify the environment details. It seems to be a compatibility issue. Commented Mar 6, 2017 at 6:20

1 Answer 1

1

You have a script named urllib2.py in the working directory. When Selenium looked up urllib2, it found that script of yours, which doesn't have a urlopen attribute. Rename that script with a unique identifier, like my_url_project.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.