简体   繁体   English

如何使用Python Selenium脚本中的此按钮定义触发javascript事件

[英]How to trigger a javascript event with this button definition from Python Selenium script

<div id="feesnav">
<div style="float: right;">
<button id="b2" class="adjustuserfees_button active" onclick="cusers(this)"/>
</

Say I want to click on button b2 using python selenium. 假设我想使用python硒单击按钮b2。 Tried all sorts of xpath definition and it retunrs can't find the locator error 尝试了各种xpath定义,并且重调谐器找不到定位器错误

The following code does not work: 以下代码不起作用:

  self.sel.fire_event("//div[@id='feesnav']//button[@id='b2']", 'click')
  or  self.sel.mouse_down("//div[@id='feesnav']//button[@id='b2']")

Any suggestions? 有什么建议么?

That is very simple I think the example detailed here , is what your looking for. 我认为这很简单,您在这里找到的示例就是您想要的。

Here is summary of what is detailed there. 这是那里详细内容的摘要。

<html>
<head>
<script type="text/javascript">
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>

<body>

<h1>My First Web Page</h1>

<p id="demo"></p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>

Goodluck. 祝好运。

Isn't there a click() method or you are saying it is not working for you? 没有click()方法,或者您说它对您不起作用吗? If you are going the javascript route you could try something like below 如果您要使用JavaScript路线,可以尝试以下操作

String myScript="var element= document.getElementById('b2');";
if(browser is not IE) //add some logic here
  myScript=myScript+"var evObj = document.createEvent('MouseEvents');evObj.initEvent('click',true,true);element.dispatchEvent(evObj);";
else
  myScript=myScript+"element.fireEvent('onclick');

selenium.getEval(myScript);

I still don't think you would need to go JS route. 我仍然认为您不需要走JS路线。 Selenium should be able to do the click for you. 硒应该能够为您完成点击。 Are you sure there are no hidden duplicate elements? 您确定没有隐藏的重复元素吗? Also there is no sync issue with your code, like you are trying to click on the button before even the page is loaded etc? 此外,您的代码也没有同步问题,例如您试图在页面加载之前就单击按钮等?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 selenium python 脚本转换为 Selenium Javascript - How to convert selenium python script in to Selenium Javascript 如何在不单击javascript中的按钮的情况下触发事件 - How to trigger an event without clicking the button in javascript 使用javascript从另一个按钮触发点击按钮事件 - Trigger a click button event from another button, using javascript JQuery / JavaScript - 从另一个按钮单击事件触发按钮单击 - JQuery / JavaScript - trigger button click from another button click event Google Apps脚本HTML-如何从整个文档(而不只是按钮)触发键盘事件? - Google Apps Script HTML - How to make a keyboard event trigger from an entire document rather than just a button? javascript触发函数来自脚本的事件,而不是来自html的显式事件 - javascript trigger function on event from a script, not explicitly from html 如何从 javascript(.js 文件)按钮单击事件调用或触发 Angular 组件方法 - How to call or trigger Angular component method from javascript (.js file) button click event 从Javascript触发ASP按钮上的“ onClick”和“ onClientClick”事件 - Trigger both 'onClick' and 'onClientClick' event on an ASP button from Javascript 如何使用Selenium和python单击Javascript按钮? - How to click Javascript button using selenium and python? 尝试从JavaScript中触发ASP.NET中的button.click()事件。 事件不会触发 - Trying to trigger a button.click() event in ASP.NET from JavaScript. Event won't trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM