Some website respond to javascript instructions and some do not. Santanders website allows javascript on the very front page but once you try to log in things change. the following passed in from python will print to console on the front page but not the second page.
driver.execute_script('console.log(" lll kkk cheaky breeki ");')
i can still just open up the developer tools and type
console.log(" lll kkk cheaky breeki ");
and it still works when i do it manually, but not when passed in by python.
my full script is below
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import selenium.common.exceptions as s_ex
driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://www.santander.co.uk/")
login_xp = '/html/body/div[3]/header/div[1]/div/nav/div/div[1]/div/div/nav/ul/li[2]/a';
time.sleep(1)
driver.execute_script('console.log(" i print fine ");')
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,login_xp)))
log = driver.find_element_by_xpath(login_xp)
log.click();
time.sleep(2)
driver.execute_script('console.log(" lll kkk cheaky breeki ");') ## how can i get this to print, why doesnt it now?
I've tried your example - it looks like the line log.click() makes the browser open a new tab. When that happens, your driver is still pointing at the original tab with the homepage.
Running driver.execute_script('console.log(" lll kkk cheaky breeki ");') at this point will execute the script in the original tab, not in the new one.
If you want to run a script on the log in page, I'd suggest going there directly using, for example:
driver.get('https://retail.santander.co.uk/olb/app/logon/access/#/logon')
The problem is that log.click() opens a new tab. So you need to tell the driver to switch to that new tab and execute all statements on that tab from now on. You will find that currently the driver also can't find any elements on the new tab as it still looking at your old tab.
log.click()
driver.switch_to.window(driver.window_handles[-1])
driver.execute_script('console.log(" lll kkk cheaky breeki ");')
Note that the tab switching is quite rudimentary, it simply goes to the last tab.