当使用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]

您在工作目录中有一个名为 urllib2.py的脚本。当硒查找urllib2时,它发现您的脚本没有urlopen属性。用唯一标识符重命名该脚本,例如my_url_project.py

最新更新