Selenium clear() command works one time per several iterations in the best case. In other cases clear() is being passed and new send_key are joining to the old one. From the other side, same command works without any problems outside of the loop.
Here is my code:
parts = []
for i in data:
current_page = driver.current_url
driver.find_element('xpath', '//*[@id="part"]').send_keys(i)
time.sleep(1)
driver.find_element('xpath', '//*[@id="search"]/input').click()
time.sleep(2)
#field = WebDriverWait(driver, 20, ignored_exceptions = StaleElementReferenceException)\
#.until(EC.presence_of_element_located((By.XPATH, '//*[@id="part"]')))
#field.clear()
try:
table = driver.find_element('xpath', '//*[@id="page"]/div[5]/div[1]/table[2]').find_element('tag name', 'tbody')
table_ = table.find_elements('tag name', 'tr')
for i in table_:
data_list = i.text.split('\n')
parts.append(data_list)
time.sleep(0.3)
except:
pass
time.sleep(1)
try:
driver.find_element('xpath', '//*[@id="part"]').clear()
text = driver.find_element('xpath', '//*[@id="part"]').text
print(text)
except:
driver.get(current_page)
url can be, for example - 'https://www.findchips.com/parametric/Filters/Data-Line-Filters?term=blm18&Manufacturer%20Part%20Number=blm18'
What I`v tried:
- Use WebDriverWait in different variations like you can see above. I
v faced StaleElementReferenceException. Usage of exception doesn
t help. - Increased timeout. Also unsuccessful.