0

I'm currently trying to remove attributes of an element on a webpage I'm automating. There will always be two variables with that name and the second one will always be the one that needs to be modified.

Does this all need to be executed at once in order to work? Or do I need to add the element as an argument? I'm unsure how to go about debugging this because I'm not extremely familiar with JavaScript.

The following is my current code:

js_one = "var x = document.getElementsByName(\"element\")"
js_two = "x[1].removeAttribute(\"readonly\")"
js_three = "x[1].removeAttribute(\"disabled\")"

driver.execute_script(js_one)
driver.execute_script(js_two)
driver.execute_script(js_three)

It gives me the following error:

  File "main.py", line 393, in main
    driver.execute_script(js_two)
  File "C:\Users\~\AppData\Local\Continuum\anaconda3\lib\site-packages\sele
nium\webdriver\remote\webdriver.py", line 629, in execute_script
    'args': converted_args})['value']
  File "C:\Users\~\AppData\Local\Continuum\anaconda3\lib\site-packages\sele
nium\webdriver\remote\webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "C:\Users\~\AppData\Local\Continuum\anaconda3\lib\site-packages\sele
nium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: ReferenceError: x is no
t defined

EDIT I solved the issue by changing my code to the following:

js_one = 'document.getElementsByName("element")[1].removeAttribute("readonly");' 
js_two = 'document.getElementsByName("element")[1].removeAttribute("disabled");'
driver.execute_script(js_one + js_two)      

If anybody has a more efficient way to carry this out, please feel free to let me know!

1
  • In general, you don't want to use JS to make elements visible, enabled, etc. You should approach the page as a user would, do whatever actions are necessary to have that element become visible or enabled on its own. When you use JS to make changes to the page like this, you are off the intended path and it could have negative side effects on the page, site, and your script. Commented Jun 5, 2018 at 17:44

1 Answer 1

2

You are executing three JavaScript snippets one after another, and in your second snippet you expect variable x to be defined, which it isn't, hence you get the ReferenceError:

selenium.common.exceptions.JavascriptException: Message: ReferenceError: x is not defined

I'd suggest trying it like this:

driver.execute_script(";".join([js_one, js_two, js_three]))

This will join your individual snippets to a single one using ; and has the selenium driver execute it.

Sign up to request clarification or add additional context in comments.

1 Comment

Pretty much clean and precise +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.