0

I got stuck to write the code in WebDriver. I have the code below :

<button class="btn" onclick="javascript:return PopulateInvestor('hdnInvestorId', 'txtInvestorName', 'txtInvestorEmail', '', '', '', '', '',false,'hdnInvestorName','hdnInvestorEmail');">Select</button>

For this I wrote the following:

JavascriptExecutor jse = (JavascriptExecutor)driver;        
jse.executeScript("document.getElement(By.xpath(\"//div[@class='modal-footer']/button[@onclick'return javascript:return PopulateInvestor('hdnInvestorId', 'txtInvestorName', 'txtInvestorEmail', '', '', '', '', '',false,'hdnInvestorName','hdnInvestorEmail');']\").click()");

But still it could not identify element and could not click the button. I can see there is select button present at the end of the code but don't know how to use it. Please advise for this.

2
  • Is it necessary to find and click the element via JavascriptExecuter?
    – Twaldigas
    Commented Oct 8, 2014 at 9:35
  • i executed using javascriptExecuter just for check in bcuz it has javascript embedded with the code so i thought of using same for the above code .Please guide me in this case Commented Oct 8, 2014 at 9:55

2 Answers 2

0

This should work:

WebElement element = driver.findElement(By.xpath("//button[@class='btn' and contains(@onclick,'PopulateInvestor')]"));
element.click();

The driver search for the element on this way

  • element has attribute class and the value of this is btn
  • element has attribute onclick and the value of this contains the string PopulateInvestor

Maybe the first point is not necessary to identify the element, but I do not know the whole HTML code.

2
  • thanx a lot Twaldigas for the solution it worked :) Commented Oct 8, 2014 at 19:54
  • @RanjanGupta Glad to hear. Please accept this answer, so every visitor can easily see that this works.
    – Twaldigas
    Commented Oct 9, 2014 at 6:59
4

Try this line of code:

Driver.findElement(By.xpath("enter your xpath")).click();

Hope it works out!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.