[英]How to get value of returned element in Selenium Python execute_script?
I using Selenium Python bindings to find some elements.我使用 Selenium Python 绑定来查找一些元素。 in some place i need to use execute_script method.
在某些地方我需要使用 execute_script 方法。 Hot to get returned values from executing this script?
很想通过执行此脚本获取返回值?
So i put:所以我说:
browser = driver.Chrome() #for example
script = "document.getElementById('element')"
browser.execute_script(script)
How to receive values into python log or variable from that script?如何从该脚本将值接收到 python 日志或变量中? Like element`s ID, text, etc.?
比如元素的 ID、文本等?
I had a problem where selenium was not able to find some element but the JavaScript console on chrome found it.我遇到了一个问题, selenium无法找到某些元素,但 chrome 上的JavaScript 控制台找到了它。
So I tried adding a " return " string in the execute script method, which in your case would be:所以我尝试在执行脚本方法中添加一个“ return ”字符串,在您的情况下是:
browser = driver.Chrome()
myElement = browser.execute_script("return document.getElementById('element_id')")
You can also do different operations like you do with Selenium Web-elements .您还可以像使用Selenium Web-elements一样执行不同的操作。
Example:例子:
browser = driver.Chrome()
elementList = browser.execute_script("return document.getElementsByClassName('element_class_name')")
elementList[0].click() # Clicks the first element from list
I hope this helps!我希望这有帮助!
I using Selenium Python bindings to find some elements.我使用 Selenium Python 绑定来查找一些元素。 in some place i need to use execute_script method.
在某些地方我需要使用 execute_script 方法。 Hot to get returned values from executing this script?
从执行此脚本中获取返回值很热吗?
So i put:所以我把:
browser = driver.Chrome() #for example
script = "document.getElementById('element')"
browser.execute_script(script)
How to receive values into python log or variable from that script?如何从该脚本接收值到 python 日志或变量中? Like element`s ID, text, etc.?
像元素的 ID、文本等?
From the js doc (python api can be found there)来自 js 文档(可以在那里找到 python api)
If the script has a return value (ie if the script contains a return statement), then the following steps will be taken for resolving this functions return value: - For a HTML element, the value will resolve to a webdriver.
如果脚本有返回值(即如果脚本包含返回语句),则将采取以下步骤来解析此函数的返回值: - 对于 HTML 元素,该值将解析为网络驱动程序。 WebElement - Null and undefined return values will resolve to null
WebElement - Null 和未定义的返回值将解析为 null
- Booleans, numbers, and strings will resolve as is Functions will resolve to their string representation - For arrays and objects, each member item will be converted according to the rules above- 布尔值、数字和字符串将按原样解析 函数将解析为它们的字符串表示形式 - 对于数组和对象,每个成员项都将根据上述规则进行转换
PS Why you need to find element using executeScript? PS 为什么需要使用 executeScript 查找元素?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.