How to click an element in Selenium WebDriver using JavaScript?

Viewed 438393

I have the following HTML:

<button name="btnG" class="gbqfb" aria-label="Google Search" id="gbqfb"><span class="gbqfi"></span></button>

My following code for clicking "Google Search" button is working well using Java in WebDriver.

driver.findElement(By.id("gbqfb")).click();

I want to use JavaScript with WebDriver to click the button. How can I do it?

12 Answers

By XPath: inspect the element on target page, copy Xpath and use the below script:worked for me.

WebElement nameInputField = driver.findElement(By.xpath("html/body/div[6]/div[1]/div[3]/div/div/div[1]/div[3]/ul/li[4]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", nameInputField);
const {Builder, By, Key, util} = require('selenium-webdriver')

// FUNÇÃO PARA PAUSA
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function example() {

    // chrome
    let driver = await new Builder().forBrowser("firefox").build()
    await driver.get('https://www.google.com.br')
    // await driver.findElement(By.name('q')).sendKeys('Selenium' ,Key.RETURN)

    await sleep(2000)

    await driver.findElement(By.name('q')).sendKeys('Selenium')

    await sleep(2000)

    // CLICAR
    driver.findElement(By.name('btnK')).click()


}
example()

Com essas últimas linhas, você pode clicar !

Another easiest solution is to use Key.RETUEN

Click here for solution in detail

driver.findElement(By.name("q")).sendKeys("Selenium Tutorial", Key.RETURN);

Use the code below, which worked for me:

public void sendKeysJavascript() {
  String file = getfile();
  WebElement browser = driver.findElement(By.xpath("//input[@type='file']"));
  JavascriptExecutor js = (JavascriptExecutor) driver;
  actionClass.waitforSeconds(5);
  js.executeScript("arguments[0].click();", browser);
  actionClass.waitforSeconds(1);
  browser.sendKeys(file);
}
String getfile() {
  return new File("./src/main/resources/TestData/example.pdf").getAbsolutePath();
}

Don't forget to add wait time before the js click action. It is mandatory

I think some parts of above codes has changed a little, I'm learning Selenium with JavaScript and I founded 2 options to click

To start we need to find the element we want to click, could be By (id, class, etc.), here is how, https://www.youtube.com/watch?v=BQ-9e13kJ58&list=PLZMWkkQEwOPl0udc9Dap2NbEAkwkdOTV3.

Right down are the 2 ways that I'm talking about:


FIRST Method: 
await driver.findElement(By.id("sampletodotext")).sendKeys("Learning Selenium", Key.RETURN);

- Here we found an empty field by it's Id, and then we write "Learning Selenium" in this field with the sendKeys().
- Key.RETURN: Simulate the person pressing the ENTER key in keyboard.

SECOND Method: 
await driver.findElement(By.id("sampletodotext")).sendKeys("Learn Selenium");
    await driver.findElement(By.id("addbutton")).click().finally();

- The difference here, is we switched the Key.RETURN of the FIRST method, for the entire second line, in the SECOND method.
Related