I would like to capture the names,id,…etc of clicked controls (input text, button,..etc) of a page (facebook.com for example). For that I am injecting a script to the page code using selenium (see code below). Everything works fine for the first open page. However, when I move to a new page by clicking on a link (for example: Create a Page: https://www.facebook.com/pages/create/?ref_type=registration_form) the script is not injected into the new open page. The code is below:
from selenium import webdriver
# Initialize the Firefox WebDriver
driver = webdriver.Firefox()
# Open the webpage
url = "https://www.facebook.com" # Replace with the URL of the webpage you want to interact with
driver.get(url)
# JavaScript code to inject
js_code = """
document.addEventListener('click', function(event) {
var target = event.target;
// Function to generate CSS selector for the element
function getCssSelector(el) {
if (!(el instanceof Element)) return;
var path = [];
while (el.nodeType === Node.ELEMENT_NODE) {
var selector = el.nodeName.toLowerCase();
if (el.id) {
selector += '#' + el.id;
path.unshift(selector);
break;
} else {
var sib = el, nth = 1;
while (sib = el.previousElementSibling) {
if (sib.nodeName.toLowerCase() == selector)
nth++;
}
if (nth != 1)
selector += ":nth-of-type("+nth+")";
}
path.unshift(selector);
el = el.parentNode;
}
return path.join(" > ");
}
// Function to extract href attribute value from the clicked link
function getHref(el) {
if (!(el instanceof HTMLAnchorElement)) return;
return el.href;
}
// Generate CSS selector for the clicked element
var cssSelector = getCssSelector(target);
// If the clicked element is a link, extract the href attribute value
var href = getHref(target);
// Print information about the clicked element
var elementInfo = {
tagName: target.tagName,
id: target.id,
className: target.className,
type: target.type,
cssSelector: cssSelector,
href: href
};
console.log('Clicked Element:', elementInfo);
alert(JSON.stringify(elementInfo));
});
"""
# Function to inject the click listener JavaScript code
def inject_click_listener_script():
driver.execute_script(js_code)
# Inject the click listener script on the initial page
inject_click_listener_script()
# Wait for user interactions
print("Please click on any element on the webpage. Press Enter when done...")
input()
# Function to wait for the page to finish loading and inject the script again
def wait_and_inject_script():
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
inject_click_listener_script()
# Monitor for page changes within the same window
while True:
# Check if the user wants to exit
exit_command = input("Press Enter to continue, or type 'exit' to quit: ")
if exit_command.lower() == "exit":
break
# Execute the script on the new page
wait_and_inject_script()
# Quit the driver
driver.quit()
it seems that the condition WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
is never meet.
Any advice or help is appreciated.