Home > Software design >  selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

Time:11-25

so I'm trying to make this bot with selenium but when I'm trying to use the send keys func it doesn't work I'm stuck on it for hours and I cant seem to find to solve the problem please if anyone has any idea I beg you to help me thanks.

print(driver.title)
tos = driver.find_element("xpath", '//*[@id="pop"]/button')
tos.click()
time.sleep(5)
name = driver.find_element("ID", "inpNick")
time.sleep(5)
name.send_keys('baby')
time.sleep(50)


driver.quit()
Traceback (most recent call last):
  File "c:\Users\SexiKiller41\Downloads\a\catchno1se.py", line 17, in <module>
    name = driver.find_element("ID", "inpNick")
  File "C:\Users\SexiKiller41\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 861, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:\Users\SexiKiller41\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "C:\Users\SexiKiller41\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
  (Session info: chrome=107.0.5304.122)
Stacktrace:
Backtrace:
    Ordinal0 [0x0039ACD3 2075859]
    Ordinal0 [0x0032EE61 1633889]
    Ordinal0 [0x0022B7BD 571325]
    Ordinal0 [0x0025A745 763717]
    Ordinal0 [0x0025AE1B 765467]
    Ordinal0 [0x0028D0F2 970994]
    Ordinal0 [0x00277364 881508]
    Ordinal0 [0x0028B56A 963946]
    Ordinal0 [0x00277136 880950]
    Ordinal0 [0x0024FEFD 720637]
    Ordinal0 [0x00250F3F 724799]
    GetHandleVerifier [0x0064EED2 2769538]
    GetHandleVerifier [0x00640D95 2711877]
    GetHandleVerifier [0x0042A03A 521194]
    GetHandleVerifier [0x00428DA0 516432]
    Ordinal0 [0x0033682C 1665068]
    Ordinal0 [0x0033B128 1683752]
    Ordinal0 [0x0033B215 1683989]
    Ordinal0 [0x00346484 1729668]
    BaseThreadInitThunk [0x7753FEF9 25]
    RtlGetAppContainerNamedObjectPath [0x77D37BBE 286]
    RtlGetAppContainerNamedObjectPath [0x77D37B8E 238]


[Done] exited with code=1 in 9.895 seconds

I was trying to enter text to an input on a website

CodePudding user response:

Try:

# id instead of ID
name = driver.find_element("id", "inpNick")  

# or
from selenium.webdriver.common.by import By
name = driver.find_element(By.ID, "inpNick")

CodePudding user response:

Instead driver.find_element("ID", "inpNick") try

driver.find_element(By.ID, "inpNick")

Also, no need to add delays between locating element and clicking it or sending keys to it.
Delays are meaningful before locating the element to make the element rendered on the page.
It is much better to use WebDriverWait expected_conditions explicit waits than hardcoded sleeps.
Improved, your code can be as following:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

print(driver.title)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="pop"]/button'))).click()
wait.until(EC.visibility_of_element_located((By.ID, "inpNick"))).send_keys('baby')
  • Related