相关文章推荐
爱旅游的小虾米  ·  Installation from ...·  2 月前    · 
精明的蘑菇  ·  TypeError:“datetime.da ...·  12 月前    · 
酒量大的钥匙  ·  正则 删除空行-掘金·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator error using find_element('username') Selenium Python

Ask Question

I am running python 3.9 with the following code. When I run the script I get the error message. Not sure what I am missing. The element is called username.

  File "/Users/user/Documents/PycharmProjects/webscrapping/app/webscraping.py", line 19, in <module>
    login = driver.find_element("username")   File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {   File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
    File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator   (Session info: chrome=98.0.4758.80) Stacktrace: 0   chromedriver                        0x0000000102a5c3c9 chromedriver + 5018569 1   chromedriver                        0x00000001029e7333 chromedriver + 4539187 2   chromedriver             0x00000001025bca88 chromedriver + 170632 3   chromedriver              0x00000001025f0d81 chromedriver + 384385 4   chromedriver              0x00000001025f14f1 chromedriver + 386289 5   chromedriver              0x00000001026238b4 chromedriver + 592052 6   chromedriver              0x000000010260e80d chromedriver + 505869 7   chromedriver              0x0000000102621604 chromedriver + 583172 8   chromedriver              0x000000010260e6d3 chromedriver + 505555 9   chromedriver              0x00000001025e495e chromedriver + 334174 10  chromedriver              0x00000001025e5935 chromedriver + 338229 11  chromedriver              0x0000000102a181ee chromedriver + 4739566 12  chromedriver             0x0000000102a31f51 chromedriver + 4845393 13  chromedriver             0x0000000102a37928 chromedriver + 4868392 14  chromedriver             0x0000000102a32a7a chromedriver + 4848250 15  chromedriver             0x0000000102a0cc31 chromedriver + 4693041 16  chromedriver             0x0000000102a4d978 chromedriver + 4958584 17  chromedriver             0x0000000102a4db01 chromedriver + 4958977 18  chromedriver             0x0000000102a63795 chromedriver + 5048213 19  libsystem_pthread.dylib  0x00007ff80f7a24f4 _pthread_start + 125 20  libsystem_pthread.dylib    0x00007ff80f79e00f thread_start + 15

Code trials:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
CURRENT_DIR = os.getcwd()
print(f"Current Dir is {CURRENT_DIR}")
driver = webdriver.Chrome(f"{CURRENT_DIR}/chromedriver")
url = 'https://somewebsite.com/login2/'
driver.get(url)
time.sleep(2)
login = driver.find_element('username')
login.send_keys('myusername@somewebsite.com')
login.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()

HTML im trying to get:

<div class="sc-dnqmqq jomEGJ" data-reactid=".0.1.0.2.1.0.0.0">
  <label class="sc-iwsKbI hOobUj" for="username" data-reactid=".0.1.0.2.1.0.0.0.0">
    <span data-reactid=".0.1.0.2.1.0.0.0.0.0">Username</span>
  </label>
                find_element('username') That function requires two arguments, but you're only providing one.
– John Gordon
                Feb 13, 2022 at 2:41
                got it.  I was missing the By.ID argument.  The tutorial I was watching did find_element_by_id but PyCharm said to use just find_element.  Thanks.
– Marvin The Martian
                Feb 13, 2022 at 2:46

find_element()

find_element() finds an element given a By strategy and a locator where both the arguments are mandatory.

So you have to pass the By class which is a set of supported locator strategies along with the locator.

Solution

Effectively your line of code will be:

  • If username is the value of class attribute:

    login = driver.find_element(By.CLASS_NAME, "username")
    
  • If username is the value of id attribute:

    login = driver.find_element(By.ID, "username")
    
  • If username is the value of name attribute:

    login = driver.find_element(By.NAME, "username")
    
  • If username is the value of linktext attribute:

    login = driver.find_element(By.LINK_TEXT, "username")
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    browser = webdriver.Chrome()
    username = "Username"
    password = "Password"
    time.sleep(5)
    url = 'web_url'
    browser.get(url)
    browser.find_element(By.ID, "txtUserName").send_keys(username) 
    browser.find_element(By.ID, "txtPassword").send_keys(password)
    time.sleep(5)
    login_btn = browser.find_element(By.ID, "Button1")
    result = login_btn.click()
    note - @txtUserName  - username btn id 
    @txtPassword  - password btn id 
    @Button1   - login btn id 
    ref - https://iq.opengenus.org/python-script-to-open-webpage-login/
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •