'Python + Selenium find_element_by_xpath returns dict

-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?



Solution 1:[1]

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

Solution 2:[2]

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...

Solution 3:[3]

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:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bk Lim
Solution 2 Vova
Solution 3 undetected Selenium