开发者

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)"/>开发者_如何学Go;
</

Say I want to click on button b2 using python selenium. Tried all sorts of xpath definition and it retunrs can't find the locator error

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? If you are going the javascript route you could try something like below

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. 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜