Python Forum

Full Version: How do I iterate over an array and perform actions using selenium chrome webdriver?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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