document.findelementbypath失败

2024-09-25 00:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用以下方法查找元素:

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

出现错误:

JavascriptException: Message: javascript error: missing ) after argument list

当我使用:

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

工作和测试通过

你能帮我看看Xpath到底出了什么问题吗


Tags: 方法selfformat元素newexecutebyposition
1条回答
网友
1楼 · 发布于 2024-09-25 00:24:56

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

你有不平衡的括号,你缺少了与self.s21.execute_javascript之后的(匹配的)。(我不知道为什么你的第二个例子没有同样的问题)

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

另外,xpath在JavaScript中是小写的

如果XPath表达式包含引号,请不要使用分隔XPath的同一种引号,然后对它们进行转义以避免它们结束包含JavaScript的字符串

self.s21.execute_javascript(
    'document.findElement(By.xpath("//div[starts-with(@class,\'ReactVirtualize\')]"))[0].scrollBy(0,8000)'.format(
        element,
        new_position
    )
)

相关问题 更多 >