简体   繁体   English

如何在 Selenium Python execute_script 中获取返回元素的值?

[英]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.

相关问题 Python Selenium如何在带参数的JavaScript元素上执行execute_script - Python selenium how to execute execute_script on a JavaScript element with arguments 如何在python和selenium中使用execute_script从下拉列表中选择一个值 - How to use execute_script in python and selenium to select a value from drop down 如何使用 Python+Selenium 从 execute_script 获取 JS 控制台响应代码? - How do I get JS Console response code from execute_script with Python+Selenium? Python和Selenium以“execute_script”来解决“ElementNotVisibleException” - Python and Selenium To “execute_script” to solve “ElementNotVisibleException” 方法execute_script无需等待脚本结束即可在python中使用硒返回值 - method execute_script don't wait end of script to return value with selenium in python 带换行符的 selenium execute_script - selenium execute_script with newlines 更改 src 属性 execute_script selenium python - Change src attribute execute_script selenium python Python Selenium:execute_script 是否可以等待 javascript 内容加载? - Python Selenium : is execute_script able to wait for javascript content to load? Python selenium execute_script ajax 请求未返回响应 - Python selenium execute_script ajax request not returning response 使用 selenium Python 运行 execute_script 的正确方法是什么? - What is the correct way to run execute_script with selenium Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM