1
def get_element():
    return browser.execute_script("return document.getElementById('dsq')")

however get_element() keeps on returning None. I was expecting it to give me a Selenium WebElement. When I execute that javascript on the browser, it gives me DOM node elements.

1
  • Why on earth are you finding an element by ID through JavaScript instead of native Selenium code? Commented Aug 21, 2014 at 18:23

1 Answer 1

1

You are trying to make a javascript DOM object equal a python selenium element. It won't be able to automatically convert like that. You would have to have code to build a Selenium version of the element from the returned object in the javascript version which you returned. Personally I would do whatever you want on the element with straight javascript and return simple types of objects that can be auto-converted (i.e. string, int, arrays of string/int etc...).

Here is a post with some help along those lines: arguments[0].click() not working for select option in selenium

If you do want to be able to execute javascript and get a DOM object and convert that to a Selenium element I would suggest that you familiarize yourself with the Selenium code base and how it does it's DOM selection specifically and how it casts that into the IWebElement interface implementation. https://code.google.com/p/selenium/source/browse/#git%2Fpy

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

2 Comments

I am using python however
Yes, but the other post is mostly about javascript which you already have the python code to execute the javascript from if you go that route. Otherwise the code link is to the python code project.

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.