5

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('[email protected]')
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>
</div>
3
  • What is the full error message? It should reference a line of code and have a traceback. Commented Feb 13, 2022 at 2:14
  • 1
    find_element('username') That function requires two arguments, but you're only providing one. Commented 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. Commented Feb 13, 2022 at 2:46

2 Answers 2

13

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")
    
Sign up to request clarification or add additional context in comments.

1 Comment

To import By from selenium.webdriver.common.by import By
0
import time
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.