Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IE-Driver] "Error executing JavaScript" when accessing TextContent attribute on re-inserted element #6538

Closed
justinttl opened this issue Oct 15, 2018 · 9 comments
Labels
D-IE I-stale Applied to issues that become stale, and eventually closed.

Comments

@justinttl
Copy link

I have encountered this issue in some custom tests while making an update from v3.4.- to v3.14.0 in order to support latest IE versions.

When attempting to call IWebElement.GetAttribute on an element which is removed and re-inserted with JavaScript, the TextContent of the element is "Error executing JavaScript".

I suspect it has something to do with this (in IEDriver ChangeLog)

v3.14.0.9
=========
 * Added ability to return text of JavaScript errors. Until this change,
   if the IE driver encountered an error in a user's JavaScript, there was
   no way to return the actual error text. This change fixes that. The text
   of the JavaScript error will be returned so it can be used as part of
   the exception thrown by the language bindings.

Meta -

OS: Windows 10
Selenium Version: 3.4.0 -> 3.14.0
Browser: IE
Browser Version: 11.285.17134.0
Bindings: C# with custom in house Python wrapper for reliability

Expected Behavior -

Prior to the update, on attribute access of the element, A StaleElementReferenceException is raised. Our custom wrapper catches any Stale pointer exceptions, and re-selects for that element. The element is now "fresh" and attribute access works as expected.

Actual Behavior -

Accessing text attribute on a re-inserted element does not raise StaleElementReferenceException.
Instead, "Error executing JavaScript" is returned.

Steps to reproduce -

  1. Open a testing HTML with a single div
  2. Select first div in Selenium => pointer
  3. Remove that div by JS
    image
  4. Re-insert
    image
  5. Access pointer text attribute by GetAttribute("textContent")
@justinttl justinttl changed the title [IE-Driver] "Error executing JavaScript" on getting attribute on [IE-Driver] "Error executing JavaScript" when accessing TextContent attribute on re-inserted element Oct 15, 2018
@jimevans
Copy link
Member

jimevans commented Oct 15, 2018

Your suspicion is incorrect, if you got your version of IEDriverServer.exe from the official release location. The version number distributed there is 3.14.0.0, and therefore does not contain the change you're referencing.

The issue is more likely that version 3.5 and above of the IE driver support the W3C WebDriver Specification. The definition of the "get element attribute" command in the spec has subtle, but very importantly, different semantics than the meaning of that command in the legacy OSS dialect of the protocol. The semantic difference is that the OSS dialect examines both attributes and properties for the appropriate value, while the W3C dialect only examines actual attributes in the markup.

Indeed, the above code (attempting to retrieve text by element.GetAttribute(textContent)) would fail in a strictly compliant implementation (since textContent is a property, not an attribute). Rather than break backward compatibility, the language bindings now currently use the JavaScript automation atom instead, to ensure the same behavior, and that is what is causing the "Error executing JavaScript" instead of the other exception.

The issue is legitimate, but it's unclear exactly what format the fix for IE should take yet.

@barancev barancev added the D-IE label Oct 16, 2018
@harshg0910
Copy link

harshg0910 commented Mar 11, 2019

I'm also facing the similar problem when instead of raising exception, it is returning "Error executing javascript" in the similar situation where the element I'm passing is stale. I'm trying to access innerHTML property though, so it looks like a generic issue. I can provide more information is needed.
iedriver version: 3.141.5

@jimevans
Copy link
Member

@harshg0910 Thank you for the offer of more information. If you could provide Selenium code along with a page (or public URL) which demonstrates the issue, that would be the information required to further debug the issue.

@harshg0910
Copy link

@jimevans sorry for delay in response, here is the python code snippet to reproduce the issue
python selenium version 3.141.0
iedriver version 3.141.5.0

from selenium import webdriver

driver = webdriver.Ie()
driver.get("http://www.w3schools.com")

element = driver.find_element_by_css_selector('p.w3-text-dark-grey')

# Get inner html of a valid element
html = driver.execute_script("return arguments[0].innerHTML", element)
print(html)  # "The language for building web pages"

# Remove element from DOM
driver.execute_script("arguments[0].parentNode.removeChild(arguments[0])", element)

# Get inner html of a stale element
try:
    html = driver.execute_script("return arguments[0].tagName", element)
except Exception as ex:
    # It throws stale pointer error for first time
    print(type(ex))
    print(str(ex))

# Bug: From now on, all call returns "Error executing JavaScript"  as string without exception
html = driver.execute_script("return arguments[0].innerHTML", element)
print(html)  # "Error executing JavaScript" without exception

@barancev
Copy link
Member

I tried this sample with Selenium Python binding 3.141.0 and IEDriverServer 3.141.5.13, the log is:

C:\devel\homework\py\venv\Scripts\python.exe C:/devel/try-something/py/my_sample.py
The language for building web pages
<class 'selenium.common.exceptions.StaleElementReferenceException'>
Message: Error executing JavaScript

Traceback (most recent call last):
  File "C:/devel/try-something/py/my_sample.py", line 24, in <module>
    html = driver.execute_script("return arguments[0].tagName", element)
  File "C:\devel\homework\py\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
    'args': converted_args})['value']
  File "C:\devel\homework\py\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\devel\homework\py\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Error executing JavaScript

So you can see it throws an exception, but it is NoSuchElementException instead of StaleElementReferenceException, that is a bug too, but a slightly different one.

@santanusk
Copy link

I am also getting the same error if the test case is running via Windows Scheduler and the "run whether user is logged on or not" is checked. The same test case is working fine when I logged in and triggering the application manually.

We have a .Net windows application by which we are navigating to our web application and login to check the application is up or not. Our application login page have some javascript\jquery to generate text box for user id and password. I logged the html source in a text file while selenium is opening the page when its been called by windows scheduler, the source does not have those textboxes. But when I debugged the same code in Visual Studio, its properly executing the script and generating the textboxes.

So it could be a security issue which is blocking javascript when a server is in ideal state.

I also used Selenium Grid and RemoteDriver but stuck with the same problem. In our case we cant use any other driver except IE.

Code

if (_service.IsRunning)
                    {
                        LogHelper.AddLog(pls.AppId, LogHelper.Level.Info, "Service is running..");
                        using (webDriver = new InternetExplorerDriver(_service, _settings))
                        {
                            LogHelper.AddLog(pls.AppId, LogHelper.Level.Info, "Opening URL [" + url + "]");
                            webDriver.Navigate().GoToUrl(url);
                            LogHelper.AddLog(pls.AppId, LogHelper.Level.Info, "URL loaded");
                            webDriver.Manage().Window.Maximize();
                            WaitUntilElementExists(webDriver, By.Id("username "));**_//Throwing timeout if running via Windows Scheduler and the "run whether user is logged on or not" is checked_**
                            webDriver.FindElement(By.Id("username")).SendKeys(userId); **_//Throwing "Error executing JavaScript" if above line is not used and running via Windows Scheduler and the "run whether user is logged on or not" is checked_**

                            webDriver.FindElement(By.Id("password")).SendKeys(pass);
                            webDriver.FindElement(By.Id("password")).SendKeys(OpenQA.Selenium.Keys.Enter);
                            LogHelper.AddLog(pls.AppId, LogHelper.Level.Info, "Login Screen");
                            //System.Threading.Thread.Sleep(2000);
                            WaitUntilElementExists(webDriver, By.Id("ctl00_obj_content_btnLogin "));
                            webDriver.FindElement(By.Id("ctl00_obj_content_btnLogin")).Click();

Server: Windows Server 2012
IE Driver: 3.141.5 (I placed the driver exe in application bin folder)
Application : .Net C#

@github-actions
Copy link

This issue is stale because it has been open 365 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the I-stale Applied to issues that become stale, and eventually closed. label Oct 20, 2021
@github-actions
Copy link

github-actions bot commented Nov 3, 2021

This issue was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this as completed Nov 3, 2021
@github-actions
Copy link

github-actions bot commented Dec 4, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
D-IE I-stale Applied to issues that become stale, and eventually closed.
Projects
None yet
Development

No branches or pull requests

5 participants