I am working on a script for a class and I execute this code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import random
import time
x = random.randrange(1, 16)
i = f"mntl-recipe-card-meta_{x}-0"
url = "https://www.eatingwell.com/recipes/17947/mealtimes/dinner/"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
link = driver.find_element(By.ID, i)
link.click()
time.sleep(2)
current_url = driver.execute_script("return window.location.href;")
print("Current URL:", current_url)
driver.quit()
print("Current URL:", driver.current_url)
driver.quit()
The code works up until it has to click on the link, then it throws a error that says “WebDriverException: Message: no such execution context”. The error seems to be thrown after I click on the link as the program stops working as the new page is loading in. I want to be able to do things after I click on the element so this stops the program from working.
Thomas Wuensch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.