6

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)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
yuzhu-chen
  • 63
  • 1
  • 1
  • 3
  • 3
    Take a few minutes to properly format your question so that the HTML and code is formatted as code and the error is formatted as a quote. It makes the question a LOT easier to read. – JeffC Mar 13 '19 at 02:46

5 Answers5

4

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();
Ali
  • 1,689
  • 1
  • 5
  • 12
2

This error message...

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)

...implies that the result of executing the xpath expression was not a node set.

Syntactically, the xpath expression you have used is a legit xpath expression which uses the text() node as in parent_element/text()="Gi2/0/20" following xpath v3.0.

Unfortunately Selenium implements xpath v1.0 which doesn't supports text() nodes.

Additionally, click() doesn't returns anything, so IP_CLICK is not needed.

Solution

To click() on the element, you can use either of the following solutions:

  • Xpath 1:

    browser.find_element_by_xpath("//span[text()='Gi2/0/20']").click()
    
  • Xpath 2:

    browser.find_element_by_xpath("//span[contains(., 'Gi2/0/20')]").click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your xpath should be:

//span[@style="display:block; overflow:hidden; white-space: nowrap" and text()="Gi2/0/20"]

It will look for span element matching the @style and text() values.

Kamal
  • 2,384
  • 1
  • 13
  • 25
0

Problem is you can not use a text node in selenium to locate an element e.g. //span/text()

You have to find an alternative to locate the element something like this

//span[contains(.,'matching_text')]

OR

//span[text()='exact _text')]

There is an alternative to locate the element using text not i.e javascriptexecutor

Here is java sample code

JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/node()[not(self::button)]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());

Refer this link for more

NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

Invalid selector: Unable to locate an element with the XPath expression 6000039318 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. Answer: I have found this error when I have passed the invalid data to the element by using sendKey() method in selenium. Like I am passing XPath of an element in-place of value(int, String) using sendKey method. This confusion made by using the same property name of data and Xpath in the property file.