First, I am a newbie in Selenium in Python. I am working on a website in Django Framework. I want to do unit tests in order to make the website project with an higher quality.
The Error
selenium.common.exceptions.JavascriptException: Message: Cyclic object value
Introduction
I do my test in tests_functionals.py which is a file containing selenium. I use XPATH in order to retrieve elements on the DOM.
In the index.html of the website, I have those links :
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/corpora">Corpora</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/approaches">Approaches</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/ML">Machine Learning</a>
</li>
</ul>
I use BaseX software to test my XPATH paths. I have sorry but I cannot show you pictures because I am new in stackoverflow so I don't have enough reputation to show pictures.
Here if ou want to learn more about BaseX
I use this path : //header/nav/div/ul/li/a/@href
And on BaseX I have this result:
href="/home"
href="/about"
href="/corpora"
href="/approaches"
href="/ML"
My problem in Django
I will show you a piece of this code in order to make you understand what I am talking.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.common.exceptions import NoSuchElementException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from sys import platform # Check the OS of the computer
from os.path import join, dirname
import time
server_used = "localhost:8000/"
class testProject(StaticLiveServerTestCase):
def hasBeenFound(self, xpath) -> bool:
try:
self.browser.find_element_by_xpath(xpath=xpath)
except NoSuchElementException:
return False
return True
def setUp(self) -> None:
if platform.startswith("win32"):
# In Windows 10
binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
self.browser = webdriver.Firefox(firefox_binary=binary, executable_path=join(dirname(__file__),'geckodriver.exe'))
else:
# In linux
self.browser = webdriver.Firefox()
def tearDown(self) -> None:
self.browser.close()
def test_nav_in_header(self):
# I connect myself into the server
self.browser.get(server_used)
# Just the beginning of the code
h1_ontology = self.browser.find_element_by_id("what-is-ontology")
# It test it the <h1>What is ontology ?</h1> exists and have this value for the text
# self.assertTrue(self.hasBeenFound(""))
self.assertEquals(
h1_ontology.find_element_by_tag_name("span").text,
"What is ontology ?",
)
toto = self.browser.find_elements_by_xpath("//header/nav/div/ul/li/a/@href")
for to in toto:
print(to)
When I want to print toto I have this error :
Traceback (most recent call last):
File "/home/adam/Documents/OntologyLearningPlatform/OLApp/tests/tests_functionals.py", line 52, in test_nav_in_header
toto = self.browser.find_elements_by_xpath("//header/nav/div/ul/li/a/@href")
File "/home/adam/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/home/adam/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1005, in find_elements
return self.execute(Command.FIND_ELEMENTS, {
File "/home/adam/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/adam/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: Cyclic object value
May you forgive me for my english level.