简体   繁体   English

在 Selenium for Python 中绕过只读 HTML 元素?

[英]Bypassing Read-only HTML elements in Selenium for Python?

I have a task I am attempting to automate in selenium using Python, which includes selecting a date.我有一个任务,我正在尝试使用 Python 在 selenium 中自动化,其中包括选择一个日期。 The date selection on the website has an input box that when you select drops down a pretty standard date table.网站上的日期选择有一个输入框,当您选择时,它会下拉一个非常标准的日期表。

The input "text" box is set to read-only and I am having some trouble getting Selenium to find the dates I am asking it to select, being able to simply send_keys of the date required to the input box would be a far easier implementation.输入“文本”框设置为只读,我在让 Selenium 找到我要求它选择的日期时遇到了一些麻烦,能够简单地将输入框所需日期的 send_keys 设置为更容易实现.

After some research, I found this solution in JS:经过一番研究,我在JS中找到了这个解决方案:

((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", element, "my text to put in the value");

Is there a similar function I could use in Python that would achieve the same?我可以在 Python 中使用类似的函数来实现相同的功能吗?

I am not that familiar with Javascript so my abilities don't allow me to convert this into Python.我对 Javascript 不太熟悉,所以我的能力不允许我将其转换为 Python。

Any help would be appreciated.任何帮助,将不胜感激。

The same function exists in selenium python and is accessible from the driver object:相同的函数存在于 selenium python 中,并且可以从驱动程序对象访问:

driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")

You say you don't know javascript, so a quick outline for you...你说你不知道 javascript,所以给你一个快速的大纲......

You're passing 3 variables to execute_script :您将 3 个变量传递给execute_script

  • First one is the js to execute arguments[0].value=arguments[1] .第一个是执行arguments[0].value=arguments[1]的 js。 This look for 2 input arguments.这将寻找 2 个输入参数。 It sets the .value of the first input with the value of the second input它将第一个输入的.value设置为第二个输入的值
  • element is the next variable you pass in and is first js argument ( argument[0] ) and is your webelement you've already identified element是您传入的下一个变量,是第一个 js 参数( argument[0] ),并且是您已经确定的 webelement
  • "my text...blah" is the second argument ( argument[1] ) and is the string value you want to set "my text...blah"是第二个参数 ( argument[1] ) 并且是您要设置的字符串值

More info on this method and others can be found in python-selenium docs .有关此方法和其他方法的更多信息可以在python-selenium docs 中找到

Execute script is covered as such:执行脚本涵盖如下:

execute_script(script, *args) Synchronously Executes JavaScript in the current window/frame. execute_script(script, *args) 在当前窗口/框架中同步执行 JavaScript。

Args: script: The JavaScript to execute. Args: 脚本:要执行的 JavaScript。 *args: Any applicable arguments for your JavaScript. *args:任何适用于您的 JavaScript 的参数。 Usage: driver.execute_script('return document.title;')用法:driver.execute_script('return document.title;')

The Selenium client equivalent line of code will be: Selenium 客户端等效的代码行将是:

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.需要注意的是JavaScript命令部分保持不变,只有方法名称根据绑定艺术而有所不同,无论是JavaPythonC#等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM