[英]JavaScript click works in console but not inside Selenium execute_script
I am using Selenium to test a site, the idea is to get all rows
from a table
, select the visible buttons
then click
them. 我正在使用Selenium来测试站点,其想法是从table
获取所有rows
,选择可见的buttons
然后click
它们。 Once clicked an event is triggered and with AJAX data is loaded right under the rows
. 单击后,将触发一个事件,并使用AJAX将数据加载到rows
正下方。
The following code works perfectly inside the Firefox console. 以下代码可在Firefox控制台中完美运行。 Actually clicks so fast that items are all loaded at once (there are max 10 rows so I would not bother to add a wait event). 实际点击速度如此之快,以至于所有项目都可以一次加载(最多有10行,因此我不必费心添加一个等待事件)。
function button_visible(row) { var opacity = row.style.opacity; if (opacity == "" || opacity == 1) { return true; } else { return false; } } var table = document.querySelectorAll('div>.table'); for (x = 1; x < table.length; x++) { row = table.item(x); var row_buttons = row.querySelectorAll('icon-button'); for (var i = 0; i < row_buttons.length; i++) { if (button_visible(row_buttons.item(i))) { row_buttons.item(i).click(); } } }
Running this JavaScript from Selenium doesn't not work: 从Selenium运行此JavaScript不起作用:
js='function button_visible(row) {var opacity = row.style.opacity; if (opacity === "" || opacity == 1) {return true;} else {return false;}} var table = document.querySelectorAll('div>.table'); for (x = 1; x < table.length; x++) {var row = table.item(x); var row_buttons = row.querySelectorAll('icon-button'); for (var i = 0; i < row_buttons.length; i++) {if(button_visible(row_buttons.item(i))){ row_buttons.item(i).click();}}}'
driver.execute_script(js)
Added come console.log
's, they show up in the console but the click event is not triggered at all. 添加了console.log
,它们显示在控制台中,但根本不会触发click事件。 Funny enough, after trying to run the code with Selenium, running the JavaScript from console does not work as well. 有趣的是,尝试使用Selenium运行代码后,从控制台运行JavaScript效果不佳。
I also tried to return these rows
as an array
and click with Selenium but this just makes things complicated as I get stale element exception. 我还尝试将这些rows
作为array
返回并单击Selenium,但这会使事情变得复杂,因为我得到了陈旧的元素异常。 To make sure it works I need to re-fetch the table rows
after each click etc. 为了确保它能正常工作,我需要在每次单击等后重新获取table rows
。
I cannot even think a reason why this would not work. 我什至无法想到为什么这行不通的原因。 Any opinions? 有什么意见吗?
Try the following: 请尝试以下操作:
driver.execute_script("""
function button_visible(row) {
var opacity = row.style.opacity;
if (opacity == "" || opacity == 1) {
return true;
} else {
return false;
}
}
var table = document.querySelectorAll('div>.table');
for (x = 1; x < table.length; x++) {
row = table.item(x);
var row_buttons = row.querySelectorAll('icon-button');
for (var i = 0; i < row_buttons.length; i++) {
if (button_visible(row_buttons.item(i))) {
row_buttons.item(i).click();
}
}
}
""")
PS: For running multiline JS in Selenium (Python) should be used """
(start and end). PS:要在Selenium(Python)中运行多行JS,应使用"""
(开始和结束)。
Hope it helps you! 希望对您有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.