Running javascript in Selenium using Python Running javascript in Selenium using Python python python

Running javascript in Selenium using Python


Try browser.execute_script instead of selenium.GetEval.

See this answer for example.


Use execute_script, here's a python example:

from selenium import webdriverdriver = webdriver.Firefox()driver.get("http://stackoverflow.com/questions/7794087/running-javascript-in-selenium-using-python") driver.execute_script("document.getElementsByClassName('comment-user')[0].click()")


If you move from iframes, you may get lost in your page, best way to execute some jquery without issue (with selenimum/python/gecko):

# 1) Get back to the main body pagedriver.switch_to.default_content()# 2) Download jquery lib file to your current folder manually & set path herewith open('./_lib/jquery-3.3.1.min.js', 'r') as jquery_js:     # 3) Read the jquery from a file    jquery = jquery_js.read()     # 4) Load jquery lib    driver.execute_script(jquery)    # 5) Execute your command     driver.execute_script('$("#myId").click()')