首页 > 解决方案 > Selenium 中的 execute_script() 有什么作用

问题描述

browser.execute_script("window.open('about:blank', 'tab2');")
browser.switch_to.window("tab2")
browser.get('http://bing.com')

我正在网上搜索在 python 中使用 selenium 打开新标签页的方法,而ctrl + t的方法在 chrome 上不起作用,所以我偶然发现了上面的代码,但是我无法理解 'excute_script' 的作用。

标签: javascriptpythonseleniumselenium-webdriverwebdriver

解决方案


execute_script方法允许执行作为字符串参数传递的 JavaScript

请注意,您可以使用 将数据从 Python 代码传递到 JavaScript 代码arguments,例如

hello = "Hello"
friends = " friends"

browser.execute_script('alert(arguments[0], arguments[1]);', (hello,  friends))

在此处输入图像描述


推荐阅读