![](/img/trans.png)
[英]I'm trying to use HTML img tag in python to send a mail, but I'm getting the following error
[英]Trying to use Selenium 2 with Python bindings, but I'm getting an import error
我剛剛通過pip install selenium
安裝了Selenium 2並且只是復制了一些示例測試以確保它正常工作:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
我將它作為test.py
保存在我Mac上的Home文件夾的子文件夾中,但是當我運行python test.py
,我得到以下輸出:
Traceback (most recent call last):
File "demo.py", line 1, in <module>
from selenium import webdriver
ImportError: cannot import name webdriver
如果我將該文件移動到我的主目錄,它可以工作。 如果你不知道,我只是開始使用Selenium和編程。 任何有關這方面的幫助將非常感激。
聽起來你的路徑中有一些名為“selenium”的其他模塊,而python正試圖導入那個模塊,因為它在你的python路徑中更早出現。 例如,您是否將文件命名為“selenium.py”?
要調試,使用簡單的import selenium
然后打印使用print selenium.__file__
導入的文件的名稱print selenium.__file__
如果你有一個名為“selenium.py”的文件,它不是正確的selenium庫,除了重命名或刪除它之外,請確保你還刪除“selenium.pyc”,否則python將繼續嘗試從.pyc導入文件。
老問題,但我也做了同樣的事情。 將我的文件命名為'selenium.py',它給出了這個非常錯誤的消息。 將文件重命名為其他內容,但仍然出現相同的錯誤。 問題是,selenium.pyc文件已經創建,因為我從終端運行腳本。 刪除.pyc文件,它就像一個魅力!
雖然這個問題在很長一段時間內似乎都處於非活動狀態,但我有相同的消息/類似問題,並且上述答案都不合適。
該網站http://kevingann.blogspot.de/2012/11/troubleshooting-pydev-and-selenium.html給出了至關重要的提示。
Selenium發生兩次,一次在系統libs中作為egg,並且“安裝”版本在外部libs中。 粉碎雞蛋就可以了。
希望這也會對某人有所幫助
錯誤ImportError: cannot import name webdriver or no module selenium2library
通過將selenium文件夾直接放在Lib
而不是site_packages
下site_packages
Pycharm中的錯誤“將selenium目錄從site-packages復制到lib后,在'導入的模塊selenium.webdriver'中找不到引用'Chrome'”。 可以如上所述進行驗證
import selenium
print (selenium.__file__)
我能夠成功運行以下代碼:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
opts = Options()
prefs = {"profile.managed_default_content_settings.images": 2}
opts.add_experimental_option("prefs", prefs)
# enter complete path of chrome driver as argument to below line of code
browser = webdriver.Chrome('C:\\Users\\BLR153\\AppData\\Local\\Programs\\Python\\Python36-32\\selenium\\chromedriver.exe')
# browser = webdriver.Firefox()
browser.get('http://www.google.com')
time.sleep(10)
browser.quit()
希望有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.