[英]selenium.common.exceptions.WebDriverException: Message: unknown error: 'script' must be a string while using execute_script() through Selenium Python
I've got an issue with browser.execute_script while using selenium with python. 在将硒与python结合使用时,browser.execute_script出现问题。 There is an element that i'd like to click (it's xpath below) 我要单击一个元素(下面是xpath)
"//*[@id='listFav_FI410_23244709400000_FAGNNURROR_IPOF_APP_P43070_W43070A_CP000A001_40']/table/tbody/tr/td[1]"
I try to do it with: 我尝试这样做:
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 :) 感谢您浪费时间在帮助我上:)
Instead of 代替
browser.execute_script(navMenu.click())
try 尝试
browser.execute_script('arguments[0].click();', navMenu)
or 要么
navMenu.click()
This error message... 此错误消息...
selenium.common.exceptions.WebDriverException: Message: unknown error: 'script' must be a string
...implies that the method execute_script()
was invoked with wrong type of parameters. ...意味着使用错误类型的参数调用了execute_script()
方法。
The execute_script()
method is defined as: 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.
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. 在您的代码试用中, executeScript()
方法将把元素的引用与要执行的方法一起作为arguments [0] (在这种情况下为click()
),此后应提供引用。 So @Andersson's solution should have worked. 因此,@ 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)
You can find a detailed discussion in What does argument [0] and argument [1] mean in javascriptexecutor in Selenium WebDriver? 您可以在Selenium WebDriver中的javascriptexecutor中的参数[0]和参数[1]表示什么中找到详细的讨论?
The hint to your main issue is the error element not visible
which implies either of the following cases: 您的主要问题的提示是错误element not visible
,这意味着以下两种情况之一:
click()
even before the element is visible/clickable 您试图在元素可见/可点击之前调用click()
click()
was invoked. 调用click()
时,元素不在视口内。 Two pottential solutions will be as follows: 两种可能的解决方案如下:
Induce WebDriverWait for the element to be clickable 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()
Use executeScript()
method to bring the element within the Viewport and then invoke click()
as follows: 使用executeScript()
方法将元素带入视口中 ,然后click()
如下所示调用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()
The correct way to execute a script is to actually write a JavaScript script! 执行脚本的正确方法是实际编写JavaScript脚本! . 。
The click()
function of selenium is on the element of the DOM you have located not a script. selenium的click()
函数位于您没有找到的DOM元素上。
As @Andersson suggested try browser.execute_script('arguments[0].click();', navMenu)
正如@Andersson建议的那样,尝试使用browser.execute_script('arguments[0].click();', navMenu)
I can see you added a sleep for 3 seconds... Using Selenium we generally use WebDriverWait
you can learn more about wait's here . 我可以看到您增加了3秒钟的睡眠时间...使用Selenium,我们通常使用WebDriverWait
您可以在此处了解更多关于wait's的信息 。
If it's too complicated you can just start with driver.implicitly_wait(3)
instead of sleep. 如果太复杂,您可以从driver.implicitly_wait(3)
开始,而不是睡眠。
Edit: 编辑:
If the Element is not displayed yet you can just use navMenu.is_displayed()
如果尚未显示Element,则可以使用navMenu.is_displayed()
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.