I Am Creating Whatsapp Script Which It Saves All My Whatsapp Contacts Name In Selenium.
Error:
Traceback (most recent call last):
File ".\main.py", line 11, in <module>
print(contact.text())
AttributeError: 'list' object has no attribute 'text'
Code:
from selenium import webdriver
from selenium.webdriver.common import keys
import time
driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://web.whatsapp.com/")
input("Qr Code Done? : ")
driver.implicitly_wait(10)
contact = driver.find_elements_by_class_name('_3q9s6')
print(contact.text()
Thanks
CodePudding user response:
find_elements
returns a list
in Selenium-Python bindings. So, you can not use .text
on a list.
Either do the following :
- use
find_element
instead.
Like :
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
If you wanna use
find_elements
, then do this :contact = driver.find_elements_by_class_name('_3q9s6') for c in contact: print(c.text)
CodePudding user response:
.find_element_*
(without s
) for finding single element, or this is matching the first element if in the web page there are multiple element with same specific:
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
But your code, .find_elements_*
(with s
), it will return a list.
You can achieve with this by accessing the index:
contact = driver.find_element_by_class_name('_3q9s6')
#first element
print(contact[0].text)
Note, to grab the text it should .text
not .text()
CodePudding user response:
find_element_by_class_name
is on plural, it finds all of the elements, try:
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
You where only a character off, close enough :-)