我的测试在我的机器上运行正常。但是,在Jenkins上,我有以下错误:
Traceback (most recent call last):
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/django/test/utils.py", line 216, in inner
return test_func(*args, **kwargs)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/twist/tests/interface/test_hello.py", line 42, in test_login
open_login_modal_btn.click()
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
我按照以下方式安装了依赖项:
$ sudo apt-get install xvfb python-pip
$ sudo pip install pyvirtualdisplay
这是我的测试代码:
class MySeleniumTests(LiveServerTestCase):
fixtures = ['user-data.json']
def setUp(self):
self.display = Display(visible=0, size=(800, 600))
self.display.start()
group = Group.objects.get_or_create(name="Test")[0]
user_adm = User.objects.create(username="adm",
is_active=True,
is_staff=True,
is_superuser=True)
user_adm.set_password("123")
user_adm.groups.add(group)
user_adm.save()
self.selenium.get('%s' % (self.live_server_url))
self.browser = self.selenium
def tearDown(self):
self.display.stop()
@classmethod
def setUpClass(cls):
super(MySeleniumTests, cls).setUpClass()
cls.selenium = WebDriver()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(MySeleniumTests, cls).tearDownClass()
@override_settings(DEBUG=True)
def test_login(self):
open_login_modal_btn = self.browser.find_element_by_xpath("//a[@class='btn btn-nav']")
open_login_modal_btn.click()
# Login info
username_html_id = "lm-email"
password_html_id = "lm-password"
login_value = "adm"
password_value = "123"
username = self.browser.find_element_by_id(username_html_id)
password = self.browser.find_element_by_id(password_html_id)
# Sending login info
username.send_keys(login_value)
password.send_keys(password_value)
desired_url = self.live_server_url + "/panel/"
form = self.browser.find_element_by_xpath('//form[@id="login-modal"]')
form.submit()
time.sleep(5)
# Testing if successful login
# Logging in
current_url = self.browser.current_url
self.assertEqual(current_url, desired_url)
有关于如何修复它的线索吗?任何有关如何调试/修复它的帮助将不胜感激! : - )
- 编辑:
我的问题已经解决,如果您遇到类似问题,请查看:WebDriver click() vs JavaScript click()我的问题就是这种情况。
答案 0 :(得分:3)
Waiting for element to be visible or clickable通常有助于克服这样的问题:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(self.browser, 10)
open_login_modal_btn = wait.until(
EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-nav']"))
)
open_login_modal_btn.click()
如果这没有帮助,可以尝试:
open_login_modal_btn = self.browser.find_element_by_xpath("//a[@class='btn btn-nav']")
self.browser.execute_script("arguments[0].scrollIntoView();", open_login_modal_btn)
移至元素并点击通过Browser Actions:
actions = ActionChains(self.browser)
actions.move_to_element(open_login_modal_btn).click().perform()
在测试开始时最大化浏览器窗口:
self.browser.maximize_window()
点击通过JavaScript (WebDriver click() vs JavaScript click()):
self.browser.execute_script("arguments[0].click();", open_login_modal_btn)