TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted using Xpath through Selenium

yuzhu-chen picture yuzhu-chen · Mar 13, 2019 · Viewed 7.2k times · Source

Part of page source:

<span style="display:block; overflow:hidden; white-space: nowrap">Gi2/0/20</span>

Parts of the code:

from selenium import webdriver
...
driver = webdriver.Chrome()
...
IP_CLICK = browser.find_element_by_xpath('//span[@style="display:block; overflow:hidden; white-space: nowrap"]/text()="Gi2/0/20"').click()

I am trying to select an element in my web page with the xpath expression, but I'm getting the following error:

InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //span[@style="display:block; overflow:hidden; white-space: nowrap"]/text()="Gi2/0/20" because of the following error:

TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=72.0.3626.121) (Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Windows NT 6.1.7601 SP1 x86_64)

Answer

Ali CSE picture Ali CSE · Mar 13, 2019

You are using the invalid xpath expression, use the below modified xpath :

IP_CLICK = browser.find_element_by_xpath("//span[text()='Gi2/0/20']");
IP_CLICK.click();

If there are multiple matches then use the indexing, I mean pass the matching index number in the below xpath :

xpath = "(//span[text()='Gi2/0/20'])[Matching index number goes here]";
IP_CLICK = browser.find_element_by_xpath(xpath);
IP_CLICK.click();