为 Python 绕过 Selenium 中的只读 HTML 元素?
Bypassing Read-only HTML elements in Selenium for Python?
我有一个任务正在尝试使用 Python 在 selenium 中自动执行,其中包括 select 一个日期。网站上的日期 selection 有一个输入框,当您 select 下拉一个非常标准的日期 table。
输入“文本”框设置为只读,我在让 Selenium 找到我要求它 select 的日期时遇到了一些问题,能够简单地 send_keys输入框所需的日期会更容易实现。
经过一番研究,我在 JS 中找到了这个解决方案:
((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", element, "my text to put in the value");
我可以在 Python 中使用可以实现相同功能的类似功能吗?
我对 Javascript 不太熟悉,所以我的能力不允许我将其转换为 Python。
如有任何帮助,我们将不胜感激。
selenium 中存在相同的功能 python 并且可以从驱动程序对象访问:
driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")
你说你不知道javascript,所以给你一个快速概述...
您将 3 个变量传递给 execute_script
:
- 第一个是要执行的js
arguments[0].value=arguments[1]
。这将查找 2 个输入参数。它将第一个输入的 .value
设置为第二个输入的值
element
是您传入的下一个变量,是第一个 js 参数 (argument[0]
) 并且是您已经确定的网络元素
"my text...blah"
是第二个参数(argument[1]
),是你要设置的字符串值
有关此方法和其他方法的更多信息,请参见 python-selenium docs。
执行脚本如下:
execute_script(script, *args) Synchronously Executes JavaScript in the
current window/frame.
Args: script: The JavaScript to execute.
*args: Any applicable arguments for your JavaScript. Usage: driver.execute_script(‘return document.title;’)
python 客户端等效代码行将是:
driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")
It is to be noted the JavaScript command part remains the same and only the method name varies as per the binding art, be it Java, Python, C#, etc.
我有一个任务正在尝试使用 Python 在 selenium 中自动执行,其中包括 select 一个日期。网站上的日期 selection 有一个输入框,当您 select 下拉一个非常标准的日期 table。
输入“文本”框设置为只读,我在让 Selenium 找到我要求它 select 的日期时遇到了一些问题,能够简单地 send_keys输入框所需的日期会更容易实现。
经过一番研究,我在 JS 中找到了这个解决方案:
((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", element, "my text to put in the value");
我可以在 Python 中使用可以实现相同功能的类似功能吗?
我对 Javascript 不太熟悉,所以我的能力不允许我将其转换为 Python。
如有任何帮助,我们将不胜感激。
selenium 中存在相同的功能 python 并且可以从驱动程序对象访问:
driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")
你说你不知道javascript,所以给你一个快速概述...
您将 3 个变量传递给 execute_script
:
- 第一个是要执行的js
arguments[0].value=arguments[1]
。这将查找 2 个输入参数。它将第一个输入的.value
设置为第二个输入的值 element
是您传入的下一个变量,是第一个 js 参数 (argument[0]
) 并且是您已经确定的网络元素"my text...blah"
是第二个参数(argument[1]
),是你要设置的字符串值
有关此方法和其他方法的更多信息,请参见 python-selenium docs。
执行脚本如下:
execute_script(script, *args) Synchronously Executes JavaScript in the current window/frame.
Args: script: The JavaScript to execute. *args: Any applicable arguments for your JavaScript. Usage: driver.execute_script(‘return document.title;’)
driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")
It is to be noted the JavaScript command part remains the same and only the method name varies as per the binding art, be it Java, Python, C#, etc.