Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object to string doesn't return expect result

I'm using selenium on Python and I am converting the result of

name = browser.find_element_by_css_selector('elementname')

to string. It is returning 'WebElement' and what I am doing to convert it is

fname = type(name).__name__

I need it to return the actual element, in which case, it'd be a username of whoever's profile 'browser' is. I just don't know why I am returned 'WebElement'.

Thanks!

(I'm sure I don't need to provide the URL and element name, right?)

like image 652
iPhynx Avatar asked Feb 18 '26 13:02

iPhynx


1 Answers

find_element_by_css_selector() method returns a WebElement instance. Instead, you need the value of the .text property:

name = browser.find_element_by_css_selector('elementname')
print(name.text)
like image 81
alecxe Avatar answered Feb 21 '26 01:02

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!