document.findElementByXPath失败

问题描述 投票:0回答:1

我想使用以下元素查找元素:

self.s2l.execute_javascript(
    'document.findElement(By.XPath"{}")[0].scrollBy(0,{})'.format(
        element,
        new_position
    )

错误:

参数列表后的[JavascriptException:消息:javascript错误:缺少]

当我使用时:

self.s2l.execute_javascript(
     'document.getElementsByClassName("{}")[0].scrollBy(0,{})'.format(
        element,
        new_position
    )

Works and test pass.

您能不能帮Xpath毁了什么?

javascript python selenium-webdriver xpath robotframework
1个回答
0
投票

您需要在By.XXX()的参数周围加上括号。另外,在.和定位符的类型之间有一个By

self.s2l.execute_javascript(
    'document.findElement(By.xpath("{}"))[0].scrollBy(0,{})'.format(
        element,
        new_position
    )

此外,xpath在JavaScript Selenium中为小写。

© www.soinside.com 2019 - 2024. All rights reserved.