click on a button with JQuery

Anik Source

There is somthing wrong with the page I want to test.

My first try: When I clicked manually on a button, then I will be forwarded normally on the next page. When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.

By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[@id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();

But I want to try a workaround:

I clicked on the same button with JQuery. I opened my chrome console and execute the button with:

jQuery('#button\\.buchung\\.continue').click()

How can I execute this JQuery expression in my selenium code?

I tried this, but without success:

JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
javajqueryseleniumselenium-webdriver

Answers

answered 3 months ago Guy #1

Use $

je.executeScript("$('#button\\.buchung\\.continue').click()");

answered 3 months ago SP. #2

jQuery("selector") will return you a list. I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)

Code:

je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()"); 

answered 3 months ago DebanjanB #3

You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :

By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

comments powered by Disqus