0

Is it possible to call a python method from javascript in selenium.

I am looking something like QWebChannel in PyQt5.

I found how can i run javascript from python. Example:

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/tutor_connect/index.php")
# to scroll till page bottom
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

But i am looking from the oposite: when a javascript event is fired or a javascript function is end then call a python method.

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • execute_script method can return a value. You could do what you like based on the return value. – pcalkins Jul 14 '22 at 20:48
  • @pcalkins Well let have a javascript click event fired. I want when the event is fired a python method to be called. How can i do this with `execute_script`? – Chris P Jul 14 '22 at 21:23
  • There are webdriver listeners you can use: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/events/WebDriverListener.html Otherwise your injected javascript would have to do something to do the DOM that you are polling for... and that would stop any other Selenium actions from happening. But if you are already using Selenium you can control the clicks, so I'm not getting something... Can you describe more specifically what you are trying to do? – pcalkins Jul 14 '22 at 22:25
  • With selenium - chromium browser i want to open a web page which has javascript webrtc implementation for transmition audio data between client (python user) and server (also python user). I want to send the audio data back to server (python user) for matplotlib plot. It's a bit confused i know, but what i am trying to do is to visualize the recevied from javascript audio data to pyqt5 application. – Chris P Jul 14 '22 at 22:41
  • I guess the bit I'm missing is why you need Selenium or Chromium. Just make the calls in Python. – pcalkins Jul 14 '22 at 23:45
  • I may want to plot web rtc audio data received from web user and display that in pyqt5 app using matplotlib. – Chris P Jul 16 '22 at 01:24

0 Answers0