JavaScriptExecutor
JavaSciptExecutor is an interface which provides mechanism to execute Javascript through selenium driver.
JavaScriptExecutor is used when Selenium Webdriver fails to click on any element due to some issue.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
// to enter text
js.executeScript ("document.getElementsByName('uploadfile_0')[0].value='C://Users/ankumalv/Desktop/testNotes.txt'");
// to click
js.executeScript("arguments[0].click();", spaceIsAWebelement);
js.executeScript("alert('Hello world - this is ankur');");
// to get docs
String url = js.executeScript("return document.URL;").toString();
js.executeScript("return document.title;").toString();
//toscroll
js.executeScript("window.scrollBy(0,600)");
And below simple prog to upload a file:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AttachmentTest {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
String baseUrl = "http";
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id(""));
uploadElement.sendKeys("C://Users/ankumalv/Desktop/testNotes.txt");
WebElement send = driver.findElement(By.name("send"));
send.click();
}
}
Comments
Post a Comment