【问题标题】:SELENIUM cant select javascript generated nameSELENIUM 无法选择 javascript 生成的名称
【发布时间】:2023-04-01 17:14:01
【问题描述】:

您好,这是我尝试在其中插入密钥的 HTML:

<input type="text" name="input[220].pk[202].name['CODICE_ORDINE_OLO'].value" alias="" value="" pattern=".*" class="form-control form-control-sm   null" maxlength="4000" data-placement="right" data-original-title="" title="">

该类用于 HTML 的其他部分,因此我无法使用它来查找它,唯一独特的是名称,但使用 xpath:

WebDriverWait(driver, 10)
driver.find_element_by_xpath("//input[@name='input[220].pk[202].name['CODICE_ORDINE_OLO'].value']").send:keys(NW)

我收到以下错误:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpa
th expression //input[@name='input[220].pk[202].name['CODICE_ORDINE_OLO'].value'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@name='input[220].pk[202].name['CODICE_ORDI
NE_OLO'].value']' is not a valid XPath expression.

有什么帮助吗?

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    如果名称是动态的,我建议使用contains

    //input[contains(@name,"input[220]")]
    

    如果名称是静态的,请使用下面的xPath

    //input[@name="input[220].pk[202].name['CODICE_ORDINE_OLO'].value"]
    

    【讨论】:

      【解决方案2】:

      这里你实际上需要在字符串中定义字符串。
      为此,您必须每次在 '" 之间切换。
      这里的整个 XPath 表达式在开头和结尾的单个撇号 ' 内,name 属性值用双撇号 " 引用,内部值 CODICE_ORDINE_OLO 用单个撇号 ' 引用再次。
      试试这个:

      driver.find_element_by_xpath('//input[@name="input[220].pk[202].name[' + 'CODICE_ORDINE_OLO' + '].value"]').send:keys(NW)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-09
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-31
        • 1970-01-01
        相关资源
        最近更新 更多