Skip to content
Advertisement

Selenium element.text() is not considered as a string

Im trying to get an element name with python selenium and print it later but it throws following error:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string

Code line looks like this:

title = driver.find_element((By.CLASS_NAME, 'mt-3')).text()

HTML element looks like this:

<h1 class="mt-3">ElementTitle</h1>

I tried searcing for element by XPath but it seems not to be a reason for error…

Advertisement

Answer

To get a web element text you should apply .text on it, not .text()
Also, there are unnecessary parenthesis here that should be removed.
So, instead of

title = driver.find_element((By.CLASS_NAME, 'mt-3')).text()

it should be

title = driver.find_element(By.CLASS_NAME, 'mt-3').text
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement