Reputation: 15
could someone kindly point out to me where I'm going wrong please?
I've looked up the documentation and I thought I set it up correctly but keep getting the error:
line 29, in <module>
username.send_keys(cred_username)
^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'send_keys'
Currently I can:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
cred_username = "username"
cred_password = "password"
s_1=Service("my driver location")
driver = webdriver.Chrome(service=s_1)
driver.get("https:website")
# Hurdle 01 Start : get past security step.
advanced = driver.find_element(By.ID, "details-button")
advanced.click()
proceed = driver.find_element(By.ID, "proceed-link")
proceed.click()
# Hurdle 01 Finish :
# Hurdle 02 Start : logging in
username = driver.find_elements(By.ID, "idUsername")
username.clear()
username.send_keys(cred_username)
password = driver.find_element(By.ID, "idPassword")
password.clear()
password.send_keys(cred_password)
password.send_keys(Keys.RETURN)
time.sleep(99)
I've tried setting it as a string but get the same error. I've also tried a variation of "send_keys_to_element(element, *keys_to_send)" I've also tried setting an xPATH but get the same results.
If anyone can point out where I'm going wrong or direct to a web page that explains where I'm going wrong I'd really appreciate it, thanks for having a look.
I thought what I had written would allow me to simply input my log-in details at this stage so I can then access a page which would allow me to upload a document. This is the only part of the process that requires input from a keyboard.
Update from answer:
Thanks for the answer [Prophet], I've removed the 's' as you suggest and tried using the XPATH again, but now have this error come up, I feel I'm close but there's something obvious I'm missing.
File "/Users/jace/Desktop/Filing_Cabinet/Python_Folder/my_phone_config01.py", line 27
username = driver.find_element(By.XPATH, "//input[@id="Username"]")
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
[Finished in 25ms with exit code 1]
Update from comment: pitcture of the inspection page
Resolved, Final update
Prophet had pointed out I may of needed to wait for the page to load fully, hence why I was having a hard time finding the element to send keys to.
Adding a wait allowed the element to actually load for my script to locate it.
# Step 03 Start : logging in
try:
username = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='idUsername']"))
)
username.send_keys(cred_username)
password = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='idPassword']"))
)
password.send_keys(cred_password)
password.send_keys(Keys.RETURN)
# Step 03 End : need to add wait commands/let the page load.
finally:
time.sleep(30)
Upvotes: 1
Views: 1327
Reputation: 33351
Your mistake is here: username = driver.find_elements(By.ID, "idUsername")
You need to use find_element
method, not find_elements
since find_element
returns a web element object so you can apply send_keys
method on it, while find_elements
returns a list of web element and you can not apply send_keys
method on a list.
UPD
As about your additional issue.
The following XPath expression can be fixed as following:
username = driver.find_element(By.XPATH, "//input[@id='Username']")
Or
username = driver.find_element(By.XPATH, '//input[@id="Username"]')
The rule is simple: in case you enclose the string with "
, the internal strings should be enclosed with '
and wise versa.
Upvotes: 2