execute_script does not run ?

95 views
Skip to first unread message

Ali Mohamed

unread,
Nov 27, 2023, 12:01:35 AM11/27/23
to Selenium Users
Hi , i have a simple java script code :
elements = document.getElementsByClassName('cUnQKe');
var elementsArray = Array.from(elements);
elementsArray.forEach(function(element) {
element.remove();
});

that should remove a section from the website , I tried this in Chrome Console and everything works well , although i tried this code in my program as an argument to execute_script function and nothing removed , so any thoughts what is wrong in my situation?


This is My code untill execute_scirpt 

import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from collections import defaultdict

if len(sys.argv) > 1: Category = sys.argv[1]
elseraise Exception("NO Category inputted")

ser = Service(r"C:\Drivers\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome(service=ser)

driver.implicitly_wait(10)
driver.get(r'https://www.google.com/')
driver.maximize_window()
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys(f"Best Recommended {Category} Books")
search_box.submit()
my_script = """
elements = document.getElementsByClassName('cUnQKe');
var elementsArray = Array.from(elements);
elementsArray.forEach(function(element) {
element.remove();
});
"""
driver.execute_script(my_script)

Suriya Elamparithy

unread,
Jan 9, 2024, 7:45:55 AMJan 9
to Selenium Users
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

if len(sys.argv) > 1:
    Category = sys.argv[1]
else:
    raise Exception("No Category inputted")


ser = Service(r"C:\Drivers\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome(service=ser)

try:

    driver.get(r'https://www.google.com/')
    driver.maximize_window()

    # Find the search box using a more reliable method (e.g., CSS selector)
    search_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="q"]')))

   
    search_box.send_keys(f"Best Recommended {Category} Books")
    search_box.submit()

    # Add a small delay to allow the search results to load
    time.sleep(3)

    # Execute the JavaScript script to remove elements

    my_script = """
    elements = document.getElementsByClassName('cUnQKe');
    var elementsArray = Array.from(elements);
    elementsArray.forEach(function(element) {
        element.remove();
    });
    """
    driver.execute_script(my_script)

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Close the browser window at the end
    driver.quit()

David

unread,
Jan 9, 2024, 5:32:04 PMJan 9
to Selenium Users
Curious why you would want to remove elements using javascript invoked by Selenium. That's not normal test procedure. We had a case before where some QA at work deleted elements in the UI, but via the browser developer console instead of via Selenium, and the testing they were doing was actually invalid test scenario.
Reply all
Reply to author
Forward
0 new messages