Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium - Error writing in field
#1
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()
Reply
#2
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.
buran write Nov-22-2024, 08:40 PM:
Clickbait link removed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium weird error julio2000 0 2,283 Feb-23-2020, 01:24 PM
Last Post: julio2000
  Selenium webdriver error WiPi 4 15,401 Feb-09-2020, 11:38 AM
Last Post: WiPi
  error when running headless selenium julio2000 2 5,994 Feb-01-2020, 12:41 PM
Last Post: julio2000
  Error clicking button with selenium julio2000 4 7,289 Jan-06-2020, 10:59 AM
Last Post: julio2000
  Selenium error with ebdriver (geckodriver) Martinelli 4 6,581 Sep-24-2019, 01:40 AM
Last Post: Martinelli
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 19,105 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Error when trying to use Selenium ejected 1 6,235 Mar-26-2019, 04:53 AM
Last Post: ejected
  Instagramlogin Error (Selenium) julian_veit 0 3,281 Jan-26-2019, 02:40 PM
Last Post: julian_veit
  selenium error : evilcode1 7 7,344 Nov-12-2018, 01:08 PM
Last Post: snippsat
  Getting error when accessing elements in a modal window of an webpage using selenium sumandas89 3 11,400 Jul-13-2018, 10:44 AM
Last Post: mlieqo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020