user299709
user299709

Reputation: 5412

Selenium: get WebElement from executing javascript

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.

Upvotes: 1

Views: 1899

Answers (1)

mutt
mutt

Reputation: 793

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

Upvotes: 1

Related Questions