I was trying to click a tab on a Salesforce org by find_element_by_xpath().click() method. I verified that the xpath on Chrome and it was able to locate the element. However the script failed with error below when it was clicking the element:
Traceback (most recent call last):
File "/Users/zhangzhao/PycharmProjects/selenium/crm-sla.py", line 17, in <module>
browser.find_element_by_xpath('//one-app-nav-bar-item-root[@data-id="0KD5f000001Jh2QGAS"]/a').click()
File "/Users/zhangzhao/PycharmProjects/selenium/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/zhangzhao/PycharmProjects/selenium/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Users/zhangzhao/PycharmProjects/selenium/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/zhangzhao/PycharmProjects/selenium/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read properties of undefined (reading 'defaultView')
(Session info: chrome=101.0.4951.41)
For debugging purposes, I added an assertion for that element instead of click() and the script worked as expected. Anyone can gimme some hints why I was getting the error? Is it sth. with the click() method? Below is my script.
from selenium import webdriver
username = "xxx"
password = "xxx"
accountName = "Aaa Aaa"
browser = webdriver.Chrome()
browser.get(baseUrl)
browser.find_element_by_id("username").send_keys(username)
browser.find_element_by_id("password").send_keys(password)
browser.find_element_by_id("Login").click()
browser.implicitly_wait(20)
browser.find_element_by_xpath('//one-app-nav-bar-item-root[@data-id="0KD5f000001Jh2QGAS"]/a').click()
browser.implicitly_wait(10)
# Add assertion to verify if text is present
# element = browser.find_element_by_xpath('//one-app-nav-bar-item-root[@data-id="0KD5f000001Jh2QGAS"]/a/span').text
# assert element == "My Account", "element was not found..."
browser.quit()