2017-03-06 5 views
1

現在、私はSeleniumライブラリのテストコードを持っています。私はそれがうまくいくかどうかを試してみたいと思って、自分でそれを編集します。しかし、それは 'インポート'の部分で間違っています。私はすでにすべてのライブラリをインストールしています。助けてください。Selenium例外を使用する場合、属性 'urlopen'なしで例外が発生します

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

以下のように例外は次のとおりです。

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] 
+0

私は 'Python-3.5、Selenium-3.0.1、Chrome-56、Chromedriver-'と同じコードを試してみました。環境の詳細を指定してください。それは互換性の問題のようです。 –

答えて

1

あなたが作業ディレクトリにurllib2.pyという名前のスクリプトを持っています。 Seleniumがurllib2を検索すると、urlopen属性を持たないあなたのスクリプトが見つかりました。スクリプトの名前をmy_url_project.pyのような一意の識別子に変更します。

関連する問題