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

时间:2019-08-06 13:32:43

标签: python selenium xpath

我是Python和编码的新手。我试图在browser.find_element_by_xpath()内部以外部值的形式传递XPath。我在浏览器控制台中使用了XPath,它是正确的,在各个地方也使用了time.sleep()来加载网页,但出现错误。我的问题是,甚至可以用此代码找到元素吗?

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)

错误消息:

  

FLQV-7734

     

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

     

selenium.common.exceptions.InvalidSelectorException:消息:无效   选择器:无法使用xpath表达式定位元素   '// a [@ title =“ FFLQV-7734”]'由于出现以下错误:TypeError:   无法对“文档”执行“评估”:结果不是节点   设置,因此无法转换为所需的类型。 (会议   信息:chrome = 72.0.3626.81)(驱动程序信息:chromedriver = 73.0.3683.68   (47787ec04b6e38e22703e856e101e840b65afe72),平台= Windows NT   10.0.14393 x86_64)

1 个答案:

答案 0 :(得分:0)

异常告诉您XPath无效。我怀疑这是您的xpath周围的多余单引号:

//postInfoAndImages.js

import imageList from "./imageUploadDisplay"

// -> do something with imageList

这可能应该起作用:

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

完成最后一行代码:

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

c = ("""//a[@title="{}"]""".format(b[i]));time.sleep(1) 之前和//字符之后没有单引号。

相关问题