TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted using Xpath through Selenium
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted using Xpath through Selenium
部分页面来源:
<span style="display:block; overflow:hidden; white-space: nowrap">Gi2/0/20</span>
部分代码:
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()
我正在尝试使用 xpath
表达式 select 我网页中的一个元素,但出现以下错误:
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)
你的 xpath 应该是:
//span[@style="display:block; overflow:hidden; white-space: nowrap" and text()="Gi2/0/20"]
它将查找 span
与 @style
和 text()
值匹配的元素。
您正在使用无效的 xpath 表达式,请使用以下修改的 xpath :
IP_CLICK = browser.find_element_by_xpath("//span[text()='Gi2/0/20']");
IP_CLICK.click();
如果有多个匹配则使用索引,我的意思是在下面的 xpath 中传递匹配的索引号:
xpath = "(//span[text()='Gi2/0/20'])[Matching index number goes here]";
IP_CLICK = browser.find_element_by_xpath(xpath);
IP_CLICK.click();
问题是您不能在 selenium 中使用文本节点来定位元素
例如//span/text()
你必须找到一个替代方法来定位像这样的元素
//span[contains(.,'matching_text')]
或
//span[text()='exact _text')]
有一个替代方法可以使用文本定位元素,而不是 javascriptexecutor
这里是java示例代码
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());
参考thislink了解更多
这个错误信息...
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)
...表示执行 xpath 表达式的结果不是节点集。
在语法上,您使用的 xpath 表达式是一个 legit xpath 表达式,它使用text()
节点,如 parent_element/text()="Gi2/0/20"
中的 xpath v3.0。
不幸的是 Selenium 实现 xpath v1.0 不支持 text()
节点。
此外,click()
没有 returns 任何东西,因此不需要 IP_CLICK
。
解决方案
到click()
元素上,可以采用以下两种解决方案之一:
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()
选择器无效:由于以下错误,无法找到具有 XPath 表达式 6000039318 的元素:
类型错误:无法在 'Document' 上执行 'evaluate':结果不是节点集,因此无法转换为所需的类型。
回答:
当我使用 selenium 中的 sendKey() 方法将无效数据传递给元素时,我发现了这个错误。就像我使用 sendKey 方法传递元素的 XPath 代替 value(int, String) 一样。这种混淆是通过在 属性 文件中使用相同的 属性 数据名称和 Xpath 造成的。
部分页面来源:
<span style="display:block; overflow:hidden; white-space: nowrap">Gi2/0/20</span>
部分代码:
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()
我正在尝试使用 xpath
表达式 select 我网页中的一个元素,但出现以下错误:
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)
你的 xpath 应该是:
//span[@style="display:block; overflow:hidden; white-space: nowrap" and text()="Gi2/0/20"]
它将查找 span
与 @style
和 text()
值匹配的元素。
您正在使用无效的 xpath 表达式,请使用以下修改的 xpath :
IP_CLICK = browser.find_element_by_xpath("//span[text()='Gi2/0/20']");
IP_CLICK.click();
如果有多个匹配则使用索引,我的意思是在下面的 xpath 中传递匹配的索引号:
xpath = "(//span[text()='Gi2/0/20'])[Matching index number goes here]";
IP_CLICK = browser.find_element_by_xpath(xpath);
IP_CLICK.click();
问题是您不能在 selenium 中使用文本节点来定位元素
例如//span/text()
你必须找到一个替代方法来定位像这样的元素
//span[contains(.,'matching_text')]
或
//span[text()='exact _text')]
有一个替代方法可以使用文本定位元素,而不是 javascriptexecutor
这里是java示例代码
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());
参考thislink了解更多
这个错误信息...
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)
...表示执行 xpath 表达式的结果不是节点集。
在语法上,您使用的 xpath 表达式是一个 legit xpath 表达式,它使用text()
节点,如 parent_element/text()="Gi2/0/20"
中的 xpath v3.0。
不幸的是 Selenium 实现 xpath v1.0 不支持 text()
节点。
此外,click()
没有 returns 任何东西,因此不需要 IP_CLICK
。
解决方案
到click()
元素上,可以采用以下两种解决方案之一:
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()
选择器无效:由于以下错误,无法找到具有 XPath 表达式 6000039318 的元素: 类型错误:无法在 'Document' 上执行 'evaluate':结果不是节点集,因此无法转换为所需的类型。 回答: 当我使用 selenium 中的 sendKey() 方法将无效数据传递给元素时,我发现了这个错误。就像我使用 sendKey 方法传递元素的 XPath 代替 value(int, String) 一样。这种混淆是通过在 属性 文件中使用相同的 属性 数据名称和 Xpath 造成的。