简体   繁体   English

XPath是正确的,但找不到Web元素

[英]XPath is correct but doesn't find the web element

I'm new to Python and coding. 我是Python和编码的新手。 I'm trying to pass an XPath in the form of an outside value inside browser.find_element_by_xpath() . 我试图在browser.find_element_by_xpath()内部以外部值的形式传递XPath。 I've used the XPath in the browser console and it is correct, I've also used time.sleep() in various places to make the web page is loaded but I get an error. 我已经在浏览器控制台中使用了XPath,它是正确的,我还在各个地方都使用了time.sleep()来加载网页,但出现错误。 My question, is it even possible to find the element with this code? 我的问题是,甚至可以用此代码找到元素吗?

a=input()
b = a.split(",")

for i in range(len(b)):

    c = ("""'//a[@title="{}"]'""".format(b[i]));time.sleep(1)
    print(c)
    print(type(c))
    act1 = browser.find_element_by_xpath(c)

Error message: 错误信息:

FLQV-7734 FLQV-7734

'//a[@title="FFLQV-7734"]' '// a [@ title =“ FFLQV-7734”]'

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression '//a[@title="FFLQV-7734"]' 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. selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:由于以下错误,无法使用xpath表达式'// a [@ title =“ FFLQV-7734”]'定位元素:TypeError:无法执行'evaluate 'on'Document':结果不是节点集,因此无法转换为所需的类型。 (Session info: chrome=72.0.3626.81) (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.14393 x86_64) (会话信息:chrome = 72.0.3626.81)(驱动程序信息:chromedriver = 73.0.3683.68(47787ec04b6e38e22703e856e101e840b65afe72),平台= Windows NT 10.0.14393 x86_64)

The exception is telling you the XPath is not valid. 例外是告诉您XPath无效。 I suspect it is the extra single quotes around your xpath: 我怀疑这是您的xpath周围多余的单引号:

"""'//a[@title="{}"]'"""
   ^                ^

This should probably work: 这可能应该工作:

"""//a[@title="{}"]"""

Making the final line of code: 编写最后一行代码:

c = ("""//a[@title="{}"]""".format(b[i]));time.sleep(1)

Without the single quotes before the // and after the ] character. //字符之前和/之后以及在[ ]字符后都没有单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM