How to click element with JavascriptExecutor?
How to click a button with the help of JavascriptExecutor?
I've been using below code for sometime, but unable to click element. WebElement link= driver.findElement(By.click();",link);
Below are the element which I want to click: Here is an example for clicking on the "Save changes" button of the page. String url = "";. String elementid = "gwt-debug-log";. String id = "gwt-debug-log-btn";. String locator = By.id(id); // Click on element '#gwt-debug-log-btn'. Actions act = new Actions(driver);. Act.moveToElement(locator).click();perform();
Note: The id is in String format, hence you have to do following code before performing click -. // Remove the existing string. Elementid = elementid.replace("gwt-debug-log-",""); id = id.
When to use JavascriptExecutor in Selenium?
It's clear from the Java code that an example is being used to demonstrate when to use the class and its methods.
However, it's not clear to me when to use JavascriptExecutor instead of WebElement.executeScript() or one of the other methods.
So I'm wondering why one would ever use JavascriptExecutor in Java and if there's some other situation where JavascriptExecutor may be useful? I think the use case for Java is more to make a stand alone script that does stuff to the page and then use JQuery to change whatever elements you want to change (eg click a button or an element on the page). You could also use Selenium to make a stand alone script that opens a new tab in the browser. Then when you want to close the tab, you could call Java to have it open a tab and run the script again to close it.
If you're trying to execute javascript on the page, JQuery is most likely going to be more efficient. If you don't need to manipulate the page (just execute javascript and take screenshots), then I think your first choice should be to use the JavascriptExecutor.
In order to make sure the page is clean, you should first try to use JavascriptExecutor and see if it works (it will fail because you are not in the page's context, but it should be the easiest way to determine that you're in the right context). If it works, then you should see if you can make it work with JQuery.
How to handle alerts using JavascriptExecutor in Selenium?
I want to handle alerts in selenium.
I am doing the following: Alert alert = driver.switchTo().alert();
Alert.accept(); This solution works fine, but it throws a warning. I want to disable that warning and handle the alert.
As per the documentation, I tried the following: Alert alert = driver.switchTo().alert();
Alert.accept(); executor.executeScript("alert('a')"); The alert is not displayed. However, the warning still happens. This is because I am using Java Web Driver 2.40. Any help?
EDIT: I modified my code as below: Alert alert = driver.switchTo().alert();
Alert.accept(); executor.executeScript("alert('a')"); alert = driver.switchTo().alert();
Alert.accept(); And this now gives me an exception as follows: javax.script.lang.NullPointerException