from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request
driver = webdriver.Chrome()
driver.get("https://anysearchwebsite.com/")
# ticker symbols array
symbols = ['spy', 'aapl', 'msft', 'amnz', 'goog']
count = 1
for i in symbols:
driver.find_element_by_css_selector("input[name='q']").send_keys(i+Keys.RETURN)
# elem.send_keys("spy")
driver.implicitly_wait(2)
imgUrl = driver.find_element_by_css_selector(".chartimg").get_attribute("src")
opener = urllib.request.build_opener()
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(imgUrl, str(count) + ".jpg")
count = count + 1
driver.back()
Just find element inside if statement and also use driver.back if required
the stale element says the Dom was changed , as you are searching something the dom changes and when you try to send value again in second iteration to the already identified element elem , it throws stale element exception
The work around is to find element inside the for loop itself.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…