How to get variable from browser context in Selenium and stop breakpoint after getting it?

51 Views Asked by At

In the browser's devtools, I've make one XHR breakpoint, when the page do XHR, it will pause at the breakpoint. Then I can print some variable in the console. But when I access the same variable with selenium it throw the variable is not defind.

Is it possible to access the same context of the devtools's console in selenium.

From the console:

t
Object { type: "POST", data: '{"comm":{"cv":4747474,"ct

From the selenium:

dirver.execute_script('''console.log(t)''')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Programs\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 884, in execute_script
    return self.execute(command, {
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
    self.error_handler.check_response(response)
  File "D:\Programs\python\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: ReferenceError: t is not defined

I've another question, for now i need to manual open the devtools and manually set breakpoint after the selenium open the page. Is it possible do make it with selenium? I've search a lot, but with no result.

1

There are 1 best solutions below

0
Yaroslavm On

If I understood you right, you can store your variable inside global, so you can get it from Selenium execution context.

So your code would do smth like:

  1. Open chrome with dev toolsbeing opened, using --auto-open-devtools-for-tabs argument
  2. Write some logic that passes your variable inside global object ('window', for example)
  3. Implement async wait function that waits for variable to be truthy.
  4. Define function that gets that variable and logs it if it is truthy.
  5. Pass that function inside wait function and await for it.
  6. Call debugger

Example:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--auto-open-devtools-for-tabs");

driver = webdriver.Chrome(chrome_options)

wait_for_var_script = """
let waitFor = async (action, pollIntervalMs = 1000) => {
  let result;
  while (!result) {
    console.log('Waiting for result');
    result = await action();
    await new Promise(resolve => setTimeout(resolve, pollIntervalMs));
  }
  return result;
};

const getSomeObject = () => {
    let _result = window.t;
    _result && console.log(_result);
    return _result;
};

setTimeout(() => window.t = { a: '1' }, 10000);
await waitFor(getSomeObject);
debugger;
"""

driver.get('https://example.com/')
driver.execute_script(wait_for_var_script)