javascript - selenium.common.exceptions.WebDriverException : Message: unknown error: 'script' must be a string while using execute_script() through Selenium Python

标签 javascript python selenium selenium-webdriver webdriver

我在将 selenium 与 python 结合使用时遇到 browser.execute_script 问题。我想单击一个元素(下面是 xpath)

"//*[@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())

它起作用了(所以它点击了所需的元素)但是在这样做之后它立即抛出了一个终止脚本的错误:

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

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

最佳答案

这个错误信息...

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

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

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 的解决方案应该有效。

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)

您可以在 What does argument [0] and argument [1] mean in javascriptexecutor in Selenium WebDriver? 中找到详细的讨论


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

  • 您正在尝试调用 click() 甚至在元素可见/可点击
  • 之前
  • 元素不在 Viewport 内当 click() 被调用时。

解决方案

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

  • 诱导 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():

    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()
    

关于javascript - selenium.common.exceptions.WebDriverException : Message: unknown error: 'script' must be a string while using execute_script() through Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52187248/

相关文章:

javascript - jquery select2 v4 - 选择选择元素上的默认选项

javascript - React,将 onClick 事件直接添加到组件而不是在所述组件内渲染元素。

python - 你能对 Django 查询集中的聚合值做额外的数学运算吗

python - Django 电子邮件后端

python-3.x - Linux服务器中的Python Selenium "Can not connect to the Service %s"% self.path

Python Selenium : How to close the tab that gets open automatically along with the tab that selenium opens

javascript - 在 2D Javascript 数组中搜索值索引

javascript - document.write ('<scr' +'ipt ... 不工作

python - Python 中的依赖倒置

ruby-on-rails - 包含文件 ruby Selenium