Executing Javascript on Selenium/PhantomJS

亡梦爱人 提交于 2019-12-05 11:39:35

The method created for executing javascript is called execute_script(), not execute():

driver.execute_script('return $("#list").DataTable().data();')

FYI, execute() is used internally for sending webdriver commands.

Note that if you want something returned by javascript code, you need to use return.

Also note that this can throw Can't find variable: $ error message. In this case, locate the element with selenium and pass it into the script:

# explicitly wait for the element to become present
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "list")))

# pass the found element into the script
jsres = driver.execute_script('return arguments[0].DataTable().data();', element)
print(jsres)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!