selenium python函数没有运行

时间:2017-03-11 10:22:11

标签: python class selenium

我有以下使用selenium的脚本,但它不起作用。它只打印出“ok3”,表明它只是在课程的第一部分。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

class Test(unittest.TestCase):
    print('ok3')
    binary = FirefoxBinary("/Users/djnewt/Desktop/Python/geckodriver")
    self.driver = webdriver.Firefox(firefox_binary=binary)
    self.driver.get("http://google.com")

    def tearDown(self):
        self.driver.quit()

    def test_selenium_1(self):
        print('ok4')
        search_field = self.driver.find_element_by_id("lst-ib")
        search_field.send_keys("Test 1. Number 1")
        search_field.submit()
        print("ok1")
        time.sleep(2)


x = Test()
x.test_selenium_1()
x.tearDown()

我无法弄清楚可能出现的问题。 firefox浏览器窗口也没有启动,并且在调用tearDown函数时函数不会停止。

2 个答案:

答案 0 :(得分:0)

更新selenium(3.3.0),firefox(52)和geckodriver(0.15)

将路径添加到路径并直接初始化Firefox()

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time

class Test(unittest.TestCase):
 def __init__(self):
      print('ok3')
      binary = FirefoxBinary("D:\\1.Manoj\\2.Soft-war\\5.Rest\\geckodriver.exe")
      self.driver = webdriver.Firefox()#firefox_binary=binary)
      self.driver.get("http://google.com")

 def tearDown(self):
    self.driver.quit()

 def test_selenium_1(self):
    print('ok4')
    search_field = self.driver.find_element_by_id("lst-ib")
    search_field.send_keys("Test 1. Number 1")
    search_field.submit()
    print("ok1")
    time.sleep(2)


x = Test()
x.test_selenium_1()
x.tearDown()

答案 1 :(得分:0)

我建议您使用selenium 2.35.1和firefox 46.0.1,因为最新版本的selenium与Firefox浏览器不兼容