0
我在運行硒測試並嘗試從同一目錄中的另一個文件調用函數時遇到問題。我已經瀏覽了導入模塊的許多主題,但我沒有運氣。當我運行test.py時,它將在wait_until_id_is_clickable處停止,並引發NoSuchElementException,它在base.py中。這個功能是成功的,如果我把它放在test.py。使用Python/Selenium從另一個文件調用函數
test.py
import unittest
import base
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action.chains import ActionChains
class Selenium_Test(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def test_community_diversity(self):
browser = self.browser
browser.get("https://python.org")
self.assertEqual(browser.title, "Welcome to Python.org")
base.wait_until_id_is_clickable("community") #Function in base.py
elem = browser.find_element_by_id("community")
hover = ActionChains(browser).move_to_element(elem)
hover.perform()
browser.implicitly_wait(1)
elem2 = elem.find_element_by_link_text("Diversity")
self.assertEqual(elem2.is_displayed(), True)
def tearDown(self):
self.browser.close()
if __name__=="__main__":
unittest.main()
base.py
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
def wait_until_id_is_clickable(element,time=10):
wait = WebDriverWait(browser, time)
try:
wait.until(EC.element_to_be_clickable((By.ID,element)))
except:
raise NoSuchElementException("Could not find element in time.")
我使用nosetests運行我的測試。任何幫助表示讚賞!
哇哦。我犯這個錯誤真的很愚蠢。非常感謝! – AlphaFoxtrot