4

-Version- Python : 3.6.8 Selenium : 3.141.0

I try this.

total_page = driver.find_element_by_xpath('Valid X Path').text
print(total_page)

but,

AttributeError : "dict' object has no attribute 'text'

I Knew.... The find_element~ function returns Dict type, not WebElement.

What could be wrong?

Vova
  • 3,117
  • 2
  • 15
  • 23
Corona
  • 71
  • 1
  • 3

3 Answers3

2

I am also facing the same issue when I am using Chromedriver. The workaround I am currently using is to use another driver (in this case Firefox works for me).

Chrome vs Firefox driver

EDIT: I tried to uninstall and reinstall the latest chromedriver and that seems to fix the problem for me as well. Latest Chromedriver

Bk Lim
  • 1,436
  • 1
  • 10
  • 7
0

Try to avoid using

total_page = driver.find_element_by_xpath('Valid X Path').text

and change on new way, coz that one will be deprecated in the future:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
el = driver.find_element(By.XPATH, "valid_xpath")
print(el.text)

anyway, I see the problem, but is not reproduced for me...

Vova
  • 3,117
  • 2
  • 15
  • 23
0

This error message...

AttributeError : "dict' object has no attribute 'text'

...implies that find_element_by_xpath() is returning a dict type of object which is a bit unexpected and something is wrong somewhere within your code.


Deep Dive

This error started showing up recently in some of the usecases:

dict_object

where as @titusfortner mentions find_element() was returning JSON Wire Protocol signature for elements instead of a w3c response from the driver.

This issue can be reproduced with w3c set as False as follows:

options.add_experimental_option('w3c', False)

Recomendations

In such cases, it is recommended:

  • Not to set w3c at all and go as per the default configurations.
  • Do not use the desired_capabilities key
  • Avoid bypassing the sandbox security unless you got a specific usecase to do so.

However, it is also important to ensure that:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352