Python Forum
How do I iterate over an array and perform actions using selenium chrome webdriver? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: How do I iterate over an array and perform actions using selenium chrome webdriver? (/thread-29633.html)



How do I iterate over an array and perform actions using selenium chrome webdriver? - master - Sep-14-2020

I want to do a search for every element in an array

My code works and searches for the first element in the array only

Any ideas why it does not work for further elements?


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.bing.com/search?q=test&form=QBLH&sp=-1&pq=test&sc=8-4&qs=n&sk=&cvid=E4AB1C52851A40EDB1B619B0CC4374A2")

time.sleep(5)

el = driver.find_element_by_xpath("//*[@id=\"sb_form_q\"]")
time.sleep(10)

codes = ['alpha', 'beta', 'gamma']
  
for code in codes:
  try:
    el.clear()
    el.send_keys(code)
    el.send_keys(Keys.RETURN)
    time.sleep(10)
  except:
    print('error')
    pass