如何在 Python webdriver 中执行 javascript [英] How to execute a javascript in a Python webdriver

查看:55
本文介绍了如何在 Python webdriver 中执行 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium 2 Python 网络驱动程序:

Selenium 2 Python webdriver:

我试图单击由于悬停效果而隐藏的元素,并正在寻找取消隐藏和能够选择元素的选项.为此,我参考了以下示例:

I was trying to click on an element which is hidden due to the hover effect and was looking for options to unhide and be able to select the element. To do this, I referred to some examples like below:

在Java中:

JavascriptExecutor je = (JavascriptExecutor) webDriver();

另一个例子:

browser.execute_script("document.getElementsByClassName('classname').style.display='block'")

当我运行上面的例子时,我得到了以下异常:

When I ran the above example, I got the following exception:

selenium.common.exceptions.WebDriverException: Message: '' 

我不确定是否必须包含任何类来执行 javascript.如果有什么遗漏,请告诉我.

I am not sure if I had to include any class for executing the javascript. Please let me know if there is anything iam missing.

推荐答案

那是因为 getElementsByClassName 返回 DOM 元素数组.如果您需要访问第一个,请将您的 JS 更改为 document.getElementsByClassName('classname')[0].style.display='block'

That is because getElementsByClassName returns array of DOM elements. If you need to access first one change your JS to document.getElementsByClassName('classname')[0].style.display='block'

这篇关于如何在 Python webdriver 中执行 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆