Home > Back-end >  Selenium throws invalid argument exception when getting an element
Selenium throws invalid argument exception when getting an element

Time:09-08

I'm trying to code a bot that automatically logins into a site. This is the code for the login(I've only put the email for now)

    try:
        WebDriverWait(bot, delay).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/form/form/div[3]/div[1]/div/div/input")))
    except TimeoutException:
        print("Loading took too much time!")

    mail = bot.find_element((By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/form/form/div[3]/div[1]/div/div/input"))

When I go to get the element with find_element(), it gives me this error:

selenium.common.exceptions.InvalidArgumentException: Message: expected value at line 1 column 11

I've looked up for some missing dependencies (like "from selenium.webdriver.common.by import By") but i got them all apparently.

CodePudding user response:

You put redundant pair of parentheses there.
Instead of

    mail = bot.find_element((By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/form/form/div[3]/div[1]/div/div/input"))

It should be

    mail = bot.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/form/form/div[3]/div[1]/div/div/input")

Also, you should improve your locators. Absolute XPaths are extremely fragile.

  • Related