0

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:

  1. Use WebDriverWait in different variations like you can see above. Iv faced StaleElementReferenceException. Usage of exception doesnt help.
  2. Increased timeout. Also unsuccessful.
Vladislav
  • 45
  • 5

2 Answers2

0

Ideally to invoke clear() you need to click on the clickable element inducing WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

try:
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='part']")))
    element.click()
    element.clear()
    # remaining lines
except:
    driver.get(current_page) 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Of cource. I`v tried this solution, but no luck. – Vladislav Aug 04 '23 at 14:23
  • @Vladislav There was a bug in the code which I've corrected. Try the updated code and let me know the status. – undetected Selenium Aug 04 '23 at 14:34
  • I have a strange feeling that this code working fine with minimized browser only. I cannot explain that... Also good with disabled. If you have any ideas why, please, let e know. – Vladislav Aug 05 '23 at 11:11
  • @Vladislav I'm not sure what you mean by _minimized browser only_ – undetected Selenium Aug 05 '23 at 12:30
  • I don`t use headless mode in this case, Chrome is physically opened. So, if I minimize Chrome window, your code works fine. Or much better at least. – Vladislav Aug 05 '23 at 14:57
  • @Vladislav Glad to be able to help you. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – undetected Selenium Aug 05 '23 at 15:34
  • @ undetected Selenium Your solution cannot fully solve problem. Even with headless mode app. 10% of data are lost (because of join previous and next input). This is very helpful as temporary solution. – Vladislav Aug 07 '23 at 08:40
-1

I found some solution, not very fast, but solved the problem.

WebDriverWait(driver, 20, ignored_exceptions = StaleElementReferenceException).until(EC.presence_of_element_located((By.XPATH, '//*[@id="part"]'))).clear()
    tresh = driver.find_element('xpath', '//*[@id="part"]').get_attribute('value')
    if len(tresh) == 0:
        pass
    else:
        while len(tresh) != 0:
            WebDriverWait(driver, 20, ignored_exceptions = StaleElementReferenceException).until(EC.presence_of_element_located((By.XPATH, '//*[@id="part"]'))).clear()
            tresh = driver.find_element('xpath', '//*[@id="part"]').get_attribute('value')

while loop until confirming that input field is empty.

I have a feeling that clear process is going much faster when browser window is minimized. I would be very grateful if anybody can comment it.

Vladislav
  • 45
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 07 '23 at 06:22