Press TAB and then ENTER key in Selenium WebDriver
Asked Answered
M

9

38

Press TAB and then ENTER key in Selenium WebDriver

GenericKeywords.typein(class.variable, PageLength); pagelength is nothing but string.

After this code, I have to give Tab key. I don't know how to give Tab key in Selenium WebDriver?

Masry answered 29/10, 2014 at 8:51 Comment(2)
TestNG has no contribute to press the key. You can do it by Selenium WebDriver, TestNG is used for test verification/assertionBuenabuenaventura
As you are using TestNG framework, it is easy to understand you are using Java language.Buenabuenaventura
R
64

Using Java:

WebElement webElement = driver.findElement(By.xpath(""));//You can use xpath, ID or name whatever you like
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);
Radtke answered 29/10, 2014 at 11:1 Comment(5)
Keys.TAB should be Keys.Tab (remove the capitilization). -suluHeterochromosome
Keys.TAB should be Key.TAB see github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/…Mahayana
what is Keys?Doorstone
@NicolasGervais it's selenium.webdriver.common.keys.KeysVerlinevermeer
Why we need element for this?Arguseyed
D
5

In javascript (node.js) this works for me:

describe('UI', function() {

describe('gets results from Bing', function() {
    this.timeout(10000);

    it('makes a search', function(done) {
        var driver = new webdriver.Builder().
        withCapabilities(webdriver.Capabilities.chrome()).
        build();


        driver.get('http://bing.com');
        var input = driver.findElement(webdriver.By.name('q'));
        input.sendKeys('something');
        input.sendKeys(webdriver.Key.ENTER);

        driver.wait(function() {
            driver.findElement(webdriver.By.className('sb_count')).
                getText().
                then(function(result) {
                  console.log('result: ', result);
                  done();
            });
        }, 8000);


    });
  });
});

For tab use webdriver.Key.TAB

Discharge answered 1/7, 2015 at 4:37 Comment(1)
Hi idophir, I'm writing automated mobile tests using Node.js and wd.js, but for some reason this doesn't work for me. In a node repl, when I type require('wd').key, it's undefined. When I type require('wd').SPECIAL_KEYS, I get a list of keys including tab. But when I do this: .sendKeys(wd.SPECIAL_KEYS.Tab) it sends a smiley face. Not amused! haha do you know if there is a way to do this with wd for mobile automation? Here's what I'm using: npmjs.com/package/wdHagride
B
3

Using Java:

private WebDriver driver = new FirefoxDriver();
WebElement element = driver.findElement(By.id("<ElementID>"));//Enter ID for the element. You can use Name, xpath, cssSelector whatever you like
element.sendKeys(Keys.TAB);
element.sendKeys(Keys.ENTER);

Using C#:

private IWebDriver driver = new FirefoxDriver();
IWebElement element = driver.FindElement(By.Name("q"));
element.SendKeys(Keys.Tab);
element.SendKeys(Keys.Enter);
Buenabuenaventura answered 1/8, 2016 at 11:33 Comment(0)
W
3

In python this work for me

self.set_your_value = "your value"

def your_method_name(self):      
    self.driver.find_element_by_name(self.set_your_value).send_keys(Keys.TAB)`
Wholehearted answered 25/6, 2019 at 22:20 Comment(0)
O
1

Be sure to include the Key in the imports...

const {Builder, By, logging, until, Key} = require('selenium-webdriver');

searchInput.sendKeys(Key.ENTER) worked great for me

Ostium answered 24/5, 2017 at 20:16 Comment(0)
E
1

Sometimes Tab will not move forward, you can use with combination of Tab and Enter keys as below

Using C# :

Driver.SwitchTo().Window(Driver.WindowHandles[1]);
IWebElement element = Driver.FindElement(By.TagName("body"));  
element.SendKeys(Keys.Tab + Keys.Enter);                
Driver.SwitchTo().Window(Driver.WindowHandles[0]);
Epigynous answered 27/7, 2021 at 10:41 Comment(0)
G
0

Here is the code to enter text and press enter after that as below:

public static void sendKeysNEnter(WebDriver wb, String sXpah, String sEnterValues, boolean bIsEnter) {
    try {
        WebElement webelement = wb.findElement(By.xpath(sXpah));
        webelement.click();
        webelement.clear();
        webelement.sendKeys(sEnterValues);
        if (bIsEnter) {
            webelement.sendKeys(Keys.ENTER);
        }
    }

    catch (Exception e) {
        e.printStackTrace();
    }
}

FYI here is the code to enter an text and then press tab as below:

public static void sendKeysNEnterTab(WebDriver wb, String sXpah, String sEnterValues, boolean bIsEnterTab) {
    try {
        WebElement webelement = wb.findElement(By.xpath(sXpah));
        webelement.click();
        webelement.clear();
        webelement.sendKeys(sEnterValues);
        if (bIsEnterTab) {
            webelement.sendKeys(Keys.TAB);
        }
    }

    catch (Exception e) {
        e.printStackTrace();
    }
}

Here is the code to press enter using webelement as below:

WebElement webelement;
webelement.sendKeys(Keys.TAB);

It perfectly works for me, Thanks.

Groundspeed answered 21/11, 2023 at 16:37 Comment(0)
H
0

for python :

# …
# add Keys and ActionChains declaration :
from selenium.webdriver import Keys, ActionChains

driver.get("https://my-web-site")
ActionChains(driver)   \
    .send_keys(Keys.TAB)\
    .perform()
ActionChains(driver)   \
    .send_keys(Keys.ENTER)\
    .perform()

selenium webdriver v4.17

see https://www.selenium.dev/documentation/webdriver/actions_api/keyboard/
and https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk/examples/python/tests/actions_api/test_keys.py#L34-L36

Humdinger answered 1/2 at 15:14 Comment(0)
G
-1
WebElement webElement = driver.findElement(By.xpath(""));

//Enter the xpath or ID.

     webElement.sendKeys("");

//Input the string to pass.

     webElement.sendKeys(Keys.TAB);

//This will enter the string which you want to pass and will press "Tab" button .

Groundspeed answered 5/8, 2016 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.