I'm trying to run a javascript file inside a Python file using Selenium WebDriver and get the return value from the javascript function. The following attempts below should work from what I've looked up online. I've worked on this for a few hours to no avail. Any help would be appreciated.
Python Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os
driver = webdriver.Chrome(executable_path='chromedriver')
driver.get('example.com')
# Clean up the text inside the <p> tags
js_file = 'clean_text.js'
with open(js_file, 'r') as f:
script = f.read()
# Attempt 1
text = driver.execute_script(script)
# Attempt 2
text = driver.execute_script(f'return {script}')
# Attempt 3
text = WebDriverWait(driver, 20).until(lambda driver: driver.execute_script(script))
# Attempt 4
text = WebDriverWait(driver, 20).until(lambda driver: driver.execute_script(f'return {script}'))
The attempts either return None or a selenium.common.exceptions.TimeoutException
Javascript Code:
$(document).ready(function() {
// Collect the text within the nested <p> tags
var text = [];
// get all nested <p> tags
let paragraphs = $(`div#id div[data-test-id=value] p`);
// Replace all whitespaces with a single white space
paragraphs.each(function(index) {
let original_text = $(this).text();
let cleaned_text = original_text.trim().replace(/\s\s+/g, ' ');
$(this).text(cleaned_text);
text.push(cleaned_text);
});
return text;
});
Given furas' answer below this JS code worked for Attempt 1 & 4. I needed to add "return" at the start, "();" at the end & remove the jQuery.
return function cleanText() {
// Collect the text within the nested <p> tags
var text = [];
// get all nested <p> tags
let paragraphs = $(`div#id div[data-test-id=value] p`);
// Replace all whitespaces with a single white space
paragraphs.each(function(index) {
let original_text = $(this).text();
let cleaned_text = original_text.trim().replace(/\s\s+/g, ' ');
$(this).text(cleaned_text);
text.push(cleaned_text);
});
return text;
}();