Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selenium throws invalid argument exception when getting an element

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading