Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

python - How to send multiple keywords in a loop to search using selenium?

I'm trying to download picture from any search website with multiple search term. This is how i have done it so far but can't find out to make it work with for in loop. Could you help me to make it work please like I want to pass in a list of keywords(symbols in my case like # elem.send_keys("spy") ) so the code iterates for each item in the list. my example for loop cause issue ;(

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request

driver = webdriver.Chrome()
driver.get("https://anysearchwebsite.com/")
elem = driver.find_element_by_id("nav-chartSearch-input")

# ticker symbols array 
symbols = ['spy', 'aapl', 'msft', 'amnz', 'goog']
count = 1
for i in symbols:
elem.send_keys(i)

# elem.send_keys("spy")


elem.send_keys(Keys.RETURN)
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

Error message is like below [12080:4620:0104/151132.576:ERROR:ssl_client_socket_impl.cc(960)] handshake failed; returned -1, SSL error code 1, net_error -113 Traceback (most recent call last):

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=87.0.4280.88)

question from:https://stackoverflow.com/questions/65557803/how-to-send-multiple-keywords-in-a-loop-to-search-using-selenium

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...