Search code examples
pythonselenium-webdrivertwitter

Can't send keys to twitter XPath


Sending keys to twitter XPath gets me error.

Twitter updated UI today, would that be the cause of the issue? Tried to find iframe to see if I need to switch but can't find any.

Traceback (most recent call last):
  File "c:\Users\user\Desktop\pyhton\test.py", line 20, in <module>
    username = Driver.find_element(By.XPATH,'//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]')  
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element     
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]"}
  (Session info: chrome=124.0.6367.158); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
        GetHandleVerifier [0x00007FF7A31F1522+60802]
        (No symbol) [0x00007FF7A316AC22]
        (No symbol) [0x00007FF7A3027CE4]
        (No symbol) [0x00007FF7A3076D4D]
        (No symbol) [0x00007FF7A3076E1C]
        (No symbol) [0x00007FF7A30BCE37]
        (No symbol) [0x00007FF7A309ABBF]
        (No symbol) [0x00007FF7A30BA224]
        (No symbol) [0x00007FF7A309A923]
        (No symbol) [0x00007FF7A3068FEC]
        (No symbol) [0x00007FF7A3069C21]
        GetHandleVerifier [0x00007FF7A34F41BD+3217949]
        GetHandleVerifier [0x00007FF7A3536157+3488183]
        GetHandleVerifier [0x00007FF7A352F0DF+3459391]
        GetHandleVerifier [0x00007FF7A32AB8E6+823622]
        (No symbol) [0x00007FF7A3175FBF]
        (No symbol) [0x00007FF7A3170EE4]
        (No symbol) [0x00007FF7A3171072]
        (No symbol) [0x00007FF7A31618C4]
        BaseThreadInitThunk [0x00007FF8056C7344+20]
        RtlUserThreadStart [0x00007FF8063626B1+33]

Code i am running:

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
Driver = webdriver.Chrome()

Driver.get('https://x.com/i/flow/login')
time.sleep(3)
username = Driver.find_element(By.XPATH,'//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]')
username.send_keys('test')
time.sleep(2)

Expect the username area to be "test".


Solution

  • //*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]
    

    Above XPath expression is incorrect. It does not locate any element. Hence no such element exception.

    The login page has only one <input> element. So you can use TAG_NAME locator strategy.

    Refer the working code below:

    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.get("https://twitter.com/i/flow/login")
    driver.maximize_window()
    wait = WebDriverWait(driver, 10)
    username = wait.until(EC.element_to_be_clickable((By.TAG_NAME, "input")))
    username.send_keys('test')
    time.sleep(10)
    

    Suggestion: Use Selenium's waits instead of time.sleep(). The last line time.sleep(10) is just so that you see your text being entered on the page, you don't need it.