-2

Please, explain the code execute_script(script, *args) in ruby Selenium web driver.

lnogueir
  • 1,859
  • 2
  • 10
  • 21
shivu2012
  • 11
  • 2
  • Please read https://meta.stackoverflow.com/a/253896/2988 to understand why "explain this code to me" questions are off-topic, and what you can do to [edit] your question to make it on-topic. Here's just some hints: explain, *precisely* which parts of the code you understand and which parts you don't understand. Explain *what it is* that hinders your understanding, show what research you have done to help you understand, and explain why and how that failed. Show which parts of the documentation you have studied, and explain which parts of the documentation you understand and which not. – Jörg W Mittag Mar 06 '21 at 07:42
  • As your question stands now, it is completely unclear what it is about that code you don't understand. Do you know what a message send is? Do you know what a method is? Do you understand the difference and the relationship between a method and a message? Do you know what an argument list is? Do you know what an argument is? Do you know what a parameter is? Do you understand the difference and the relationship between a parameter and an argument? Do you understand the syntax of Ruby? Do you understand the semantics of Ruby? Do you know what an array is? And so on. – Jörg W Mittag Mar 06 '21 at 07:44

1 Answers1

0

This allows you to execute javascript on the page as if you were in the console window of your browser.

The script parameter is the text of the script or command you want to run on the page, and the *args argument is for any arguments that script might take. This allows you to pass in other objects and data to a script. For example:

driver.execute_script("return arguments[0].text;", my_element)

This gets the text of a previously-found element on the page, that can be passed into the script as a parameter. Any amount of arguments can be passed to the script, and they are accessed in the script as arguments[i], where i=0 is the first argument after the script.

Jarom C
  • 46
  • 3
  • Thank you Jarom C. Can you give an example to only script without passing any arguments to scroll into view to any element? – shivu2012 Mar 05 '21 at 23:47
  • It shows no method error if I don’t pass any argument. – shivu2012 Mar 05 '21 at 23:58
  • In order to scroll to "any element", you must pass that element as an argument to the script. I can point you in the right direction, but you'll want to read some documentation on javascript. Read the docs for javascript's "scrollIntoView" method. If you're still stuck, check out this Stack Overflow question: https://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium – Jarom C Mar 07 '21 at 01:43