selenium.common.exceptions.WebDriverException:消息:未知错误:通过 Selenium Python 使用 execute_script() 时“脚本"必须是字符串 [英] selenium.common.exceptions.WebDriverException: Message: unknown error: 'script' must be a string while using execute_script() through Selenium Python

查看:33
本文介绍了selenium.common.exceptions.WebDriverException:消息:未知错误:通过 Selenium Python 使用 execute_script() 时“脚本"必须是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 中使用 selenium 时,我遇到了 browser.execute_script 的问题.有一个我想点击的元素(下面是 xpath)

I've got an issue with browser.execute_script while using selenium with python. There is an element that i'd like to click (it's xpath below)

"//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]"

我尝试这样做:

navMenu = browser.find_element_by_xpath("//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]")
time.sleep(3)
browser.execute_script(navMenu.click())

它可以工作(所以它点击了想要的元素),但是在执行它之后它会抛出一个错误来终止脚本:

And it works (So it clicks desired element) but right after doing it it throws an error that terminates the script:

selenium.common.exceptions.WebDriverException: Message: unknown error: 'script' must be a string

我做错了什么?有没有办法跳过这个错误?感谢您浪费时间帮助我 :)

What am I doing wrong? Is there a way to skip this error? Thx for wasting your time on helping me :)

推荐答案

此错误信息...

selenium.common.exceptions.WebDriverException: Message: unknown error: 'script' must be a string

...暗示方法 execute_script() 是用错误类型的参数调用的.

...implies that the method execute_script() was invoked with wrong type of parameters.

execute_script() 方法定义为:

execute_script(script, *args)
    Synchronously Executes JavaScript in the current window/frame.

Where:
    script: The JavaScript to execute
    *args: Any applicable arguments for your JavaScript.

在您的代码试验中,executeScript() 方法会将元素的引用作为 arguments[0] 以及要执行的方法(在这种情况下 click()) 并且此后应提供参考.所以@Andersson 的解决方案应该有效.

In your code trial executeScript() method will take the reference of the element as arguments[0] along with the method to be performed (in this case click()) and the reference should be provided thereafter. So @Andersson's solution should have worked.

navMenu = browser.find_element_by_xpath("//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]")
browser.execute_script("arguments[0].click()", navMenu)

您可以在 Selenium WebDriver 中的 javascriptexecutor 中的参数 [0] 和参数 [1] 是什么意思?

提示您的主要问题是错误 element not visible 这意味着以下任一情况:

The hint to your main issue is the error element not visible which implies either of the following cases:

  • 您试图在元素可见/可点击
  • 之前调用click()
  • click() 时,元素不在 视口内代码> 被调用.
  • You are trying to invoke click() even before the element is visible/clickable
  • Element is not within the Viewport when click() was invoked.

两种可能的解决方案如下:

Two pottential solutions will be as follows:

  • 诱导 WebDriverWait 使 元素可点击,如下所示:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# other lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]"))).click()

  • 使用 executeScript() 方法将元素引入 Viewport 然后调用 click() 如下:

  • Use executeScript() method to bring the element within the Viewport and then invoke click() as follows:

    navMenu = browser.find_element_by_xpath("//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]")
    browser.execute_script("arguments[0].scrollIntoView(true);",navMenu);
    navMenu.click()
    

  • 这篇关于selenium.common.exceptions.WebDriverException:消息:未知错误:通过 Selenium Python 使用 execute_script() 时“脚本"必须是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆