【问题标题】:How to modify selenium xpath with python如何使用 python 修改 selenium xpath
【发布时间】:2015-03-30 18:17:38
【问题描述】:
driver.find_element_by_xpath("/html/body/div[3]/div[3]/div[2]/div/div[2]/div[1]/div/div[3]").text

除了这个,我想要类似的东西

driver.find_element_by_xpath("/html/body/div[3]/div[3]/div[",i,"]/div/div[2]/div[1]/div/div[3]").text

这会在循环中增加i。但是当我使用这个时,我得到的错误信息是这个

“InvalidSelectorException:消息:传入的定位器值无效”

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    您对字符串连接使用了错误的语法。尝试替换 i 的值:

    driver.find_element_by_xpath("/html/body/div[3]/div[3]/div[%d]/div/div[2]/div[1]/div/div[3]" % i).text
    

    【讨论】:

    • @zahedae 它在打印时起作用,因为print() 接受多个参数,并在幕后将它们连接起来。对于其他一切,您需要使用string1 + string2"foo" + "bar"" ".join(string1, string2)"foo %s bar" % "baz"str.format() 方法之一,可能还有一些我错过的方法。 Google python 字符串连接和字符串格式化以获取更多信息
    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多