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

3
  • 1
    How is this a python question? Commented Aug 31, 2011 at 5:08
  • you might want to label this javascript and html question. Commented Aug 31, 2011 at 5:11
  • it's a selenium question Commented Aug 31, 2011 at 5:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.