Python Forum

Full Version: Selenium - Error writing in field
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I’m working on a script that clicks on a field and writes data from a DataFrame. The click seems to work fine because the cursor appears in the field, but when the script starts typing, the text goes to the browser’s search bar instead of the intended field. I suspect this happens because a new tab is opened beforehand, causing the focus to shift to the browser's search bar.

Does anyone know what might be causing this issue or how to fix it?

Thanks!

try:
    driver.get(url)
    time.sleep(2)
    username_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME, "username"))
    )
    username_input.send_keys(username)
    continue_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="user-login-submit"]'))
    )
    continue_button.click()
    print("Please write your password and click in 'Login'.")
    WebDriverWait(driver, 300).until(
        EC.presence_of_element_located((By.XPATH, "//span[text()='Library']"))
    )
    time.sleep(3)
    url2 = 'https://builder.onplan.app/models/strategytasks?id=877'
    driver.get(url2)
    time.sleep(5)
    current_url = driver.current_url

    for inspection in columns['Inspection']:
        try:  
            driver.switch_to.new_window('tab')
            driver.get(current_url)
            time.sleep(3)

            created_element = WebDriverWait(driver, 20).until(
                EC.element_to_be_clickable((By.XPATH, f"//a[normalize-space(text())='{inspection}']"))
            )
            driver.execute_script("arguments[0].scrollIntoView(true);", created_element)
            time.sleep(1)  # Esperar un momento
            created_element.click()
            driver.switch_to.window(driver.window_handles[-1])
            time.sleep(5)
            driver.execute_script("document.body.focus();")
            time.sleep(10)

            ######### TOOLS #########################

            tools_tab = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.ID, "tools__tab"))
            )
            tools_tab.click()
            time.sleep(5)
            for tool in df2["Tools"]:
                time.sleep(10)
                add_tool_button = WebDriverWait(driver, 10).until(
                    EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-default' and @aria-controls='toolsTable']"))
                )
                add_tool_button.click()
                time.sleep(5)
                field_search3 = driver.find_element("xpath", "//span[contains(@class, 'select2-selection__placeholder') and text()='Search or enter a tool number']")
                field_search3.click()
                time.sleep(10)
                keyboard.type(tool)
                time.sleep(5)
                keyboard.press(Key.down)
                keyboard.release(Key.down)
                time.sleep(5)
                keyboard.press(Key.enter)
                keyboard.release(Key.enter)
                add_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "addToolForm__add")))
                add_button.click()
                time.sleep(5)
                done_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "addToolForm__done")))
                done_button.click()
                time.sleep(5)
                driver.refresh()
                time.sleep(5)
            time.sleep(20)        
        except Exception as e:
            print(f"Error in element '{inspection}': {str(e)}")
except Exception as e:
    print(f"Error: {str(e)}")

input("Press enter to close browser")
driver.quit()
Hi there!

It looks like the issue you're experiencing is because when a new tab opens, the focus shifts away from the intended input field. To solve this, you need to make sure your script is set to focus on the correct tab or window before you start typing.

After opening the new tab and navigating to the desired page, you should add a line of code to switch back to the original window or the specific tab where you want to type. You can do this by using driver.switch_to.window(driver.window_handles[index]), where index is the position of the tab you want to focus on. Make sure to replace index with the correct number, usually 0 for the first tab.