I am faced with the challenge of incorporating a dynamic function after the page has loaded. This need arises because the function name is dependent on a variable obtained through parsing HTML. Presently, my approach involves:
Context: Using Selenium 3.3.1 with PhantomJS 1.9.8
((JavascriptExecutor) driver).executeScript(" eval('function getVoteX1() { return 1; }')");
String testScript = (String)((JavascriptExecutor) driver).executeScript(" getVoteX1()");
System.out.println(testScript);
However, upon executing this code, an error is thrown :
Caused by: org.openqa.selenium.WebDriverException: {"errorMessage":"Can't find variable: getVoteX1","request":{"}
It is important to note that while I currently use 'getVoteX1' as a placeholder, it will eventually be substituted with another variable in a more complex scenario.