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