我一直在与Selenium发生“名称”错误。我究竟做错了什么?

时间:2016-12-08 12:47:56

标签: python selenium selenium-webdriver

我做错了什么?我一直收到这个错误:

  

============== RESTART:C:\ Users \ Redacted \ Desktop \ Autonav_1.py ==============
  Ë       ================================================== ====================       错误:test_autonav_1(主要 .Autonav_1)       -------------------------------------------------- --------------------       Traceback(最近一次调用最后一次):         在setUp中输入文件“C:\ Users \ Redacted \ Desktop \ Autonav_1.py”,第8行           self.selenium = selenium(“localhost”,4444,“* firefox”,“Redacted”)       NameError:名称'selenium'未定义

     

在0.003s中进行1次测试        失败(错误= 1)

     

代码:

# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest, time, re

class Autonav_1(unittest.TestCase):
def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*firefox" "redacted")
    self.selenium.start() 


def test_autonav_1(self):
    sel = self.selenium
    sel.open("/hub/login.php")
    sel.type("id=Email", "redacted")
    sel.type("id=Password", "redacted")
    sel.click("id=Login")
    sel.wait_for_page_to_load("30000")
    sel.click("link=Internet Retailing")
    sel.wait_for_page_to_load("30000")
    sel.click("id=redacted")
    sel.click("id=redacted")
    sel.wait_for_page_to_load("30000")

def tearDown(self):
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()

2 个答案:

答案 0 :(得分:1)

selenium是图书馆。您需要使用webdriver

def setUp(self):
    self.driver = webdriver.Firefox("localhost", 4444, "*firefox" "redacted")

def test_autonav_1(self):
    driver = self.driver

答案 1 :(得分:0)

您要从public async Task<List<T>> GetAsync(string WebServiceUrl){ var httpClient = new HttpClient(); var json = await httpClient.GetStringAsync(WebServiceUrl); Debug.WriteLine(WebServiceUrl); Debug.WriteLine(json); var taskModels = JsonConvert.DeserializeObject<List<T>>(json); return taskModels; } 库导入webdriver,因此名称selenium不会绑定任何内容。

selenium

import selenium.webdriver

driver = selenium.webdriver.Chrome('localhost', 4444, '*firefox', '...')

from selenium import webdriver driver = webdriver.Chrome('localhost', 4444, '*firefox', '...') 指令为import关键字右侧的任何内容引入了名称绑定。在第一个示例中,绑定了两个名称 - importselenium。在第二个示例中,只绑定了一个名称。有关详细信息,请参阅python docs on import