0

Is it possible with Python Selenium to have the script run passively in the background until certain page criteria are met, then have the script take over active control of the browser (using Chrome browser, if relevant).

I have been trying to find something like an 'on_page_load()' event that would run on every page load (which ideally would check for whether specific elements are present on the page) and which would then branch to a method that takes over control of the browser. No luck with this so far.

MidnightThoughtful
  • 233
  • 1
  • 5
  • 13
  • Why do you want `Selenium` to run passively in the background ? Can you sum up your exact `Manual Steps` you are trying to Automate? – undetected Selenium Nov 27 '17 at 06:01
  • Happy to leave this question as is, but I don't agree this is a duplicate of the page linked by COLDSPEED. I am not asking how to use Selenium to request a page and wait until it is loaded before executing something else. I am asking if it is possible using Python / Selenium to hook into a 'page_load' event that gets executed by every page so I can include tests for when the user is presented with specific information on the page. I suspect Selenium is not the right tool for this task. – MidnightThoughtful Nov 27 '17 at 09:11
  • @DebanjanB Use case is we have a web portal that Users use to review work cases. When they see specific items in the work case, the user copies the content and saves it to a file. I would like to build a solution that allows our users to use the web portal per normal, but which automatically saves key information if the page contains it. The information in question adheres to rules that would be trivial to detect with regexen. As above, Selenium may not be the ideal tool for this, however I like that it renders the page exactly as the user experiences it (i.e., with JS libraries loaded etc) – MidnightThoughtful Nov 27 '17 at 09:15
  • So where are you stuck exactly? – undetected Selenium Nov 27 '17 at 09:17
  • @DebanjanB I can't find an example anywhere of Python code that starts Selenium and subscribes to a page load event. All I can find are examples, such as the one linked by COLDSPEED, where it looks like the code drives the whole navigation process, in a procedural way, from start to end. – MidnightThoughtful Nov 27 '17 at 09:21

1 Answers1

0

You can make use javascript runner of selenium for this.

driver.execute_script("
     window.onload = (function () {// logic here})();")

You can also implement some kind of listner whenever the element is available or a callback.

def hello(value, callback):
print value
callback()

def hi():
print "Hi"

hello(“hello”, hi)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Prakash Palnati
  • 3,231
  • 22
  • 35