简体   繁体   English

JavaScript单击在控制台中有效,但在Selenium execute_script中无效

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

相关问题 无法使用execute_script Selenium用javascript单击按钮 - Can't click a button with javascript using execute_script Selenium Python Selenium如何在带参数的JavaScript元素上执行execute_script - Python selenium how to execute execute_script on a JavaScript element with arguments 带换行符的 selenium execute_script - selenium execute_script with newlines 通过Selenium在Python中运行带有变量的execute_script - Running execute_script with variable inside in Python via Selenium Python Selenium:execute_script 是否可以等待 javascript 内容加载? - Python Selenium : is execute_script able to wait for javascript content to load? 如何在类似于execute_script的控制台中运行javascript - how to run javascript in console similar to execute_script Python和Selenium以“execute_script”来解决“ElementNotVisibleException” - Python and Selenium To “execute_script” to solve “ElementNotVisibleException” Selenium 中的 execute_script() 有什么作用 - What does execute_script() in Selenium does Selenium Webdriver:execute_script无法执行自定义方法和外部javascript文件 - Selenium Webdriver: execute_script can't execute custom methods and external javascript files Seleneium 异常 - arguments[0].click 不是在 Selenium Python 中使用 execute_script() 的函数 - Seleneium exceptions - arguments[0].click is not a function using execute_script() in Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM