Si è verificata un'eccezione: l'oggetto TypeError 'WebElement' non è sottoscrivibile
Ciao ragazzi, sono davvero un principiante di Python e sto solo scrivendo un pezzo di codice che apre Whatsapp
e gli dai il nome della persona e il messaggio invia quante volte vuoi.
Ma quando inizio il debug del codice mi dà questo:
Exception has occurred: TypeError 'WebElement' object is not subscriptable File "E:\Iliya\My Courses\Python\Projects\Whatsapp Robot\Whatsapp_Bot.py", line 15, in <module> msg = driver.find_element_by_class_name('_3FRCZ')[1]
# ======================================
from selenium import webdriver
PATH = 'C:\\Program Files (x86)\\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://web.whatsapp.com/')
input("Please Press The 'Enter' Button... ")
name = input("Enter Person's Name: ")
msg = input("Enter The Message: ")
counter = int(input("How Many Times Do You Want To Repeat The Message?: "))
user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))
user.click()
msg = driver.find_element_by_class_name('_3FRCZ')[1]
for i in range(counter):
msg.send_keys(msg)
button = driver.find_element_by_class_name('_1U1xa')[0]
button.click()
ragazzi per favore qualcuno bravo in Python mi risponda !!! 🙏🙏
Risposte
find_element_by_class_name ()
find_element_by_class_name() trova un elemento in base al nome della classe.
Nella riga di codice:
msg = driver.find_element_by_class_name('_3FRCZ')[1]
driver.find_element_by_class_name('_3FRCZ')
restituirebbe un singolo WebElement . Quindi non sarai in grado di allegare un indice ad esso o in altre parole renderlo sottoscrivibile.
Soluzione
Esistono due soluzioni:
Rimuovi l' indice, ovvero il
[1]
tuo codice andrà bene.In alternativa, invece di
driver.find_element_by_class_name()
utilizzarefind_elements_by_class_name()
. Quindi efficacemente la tua riga di codice sarà:msg = driver.find_elements_by_class_name('_3FRCZ')[1]