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);
Answer from Sachin Francis on Stack OverflowUsing 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);
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
How to press "TAB" then "ENTER" key in Selenium WebDriver using Java? - Stack Overflow
python - Send multiple tab key presses with Selenium - Stack Overflow
Press TAB and then ENTER key in Selenium WebDriver with Ruby - Stack Overflow
Selenium Powershell Send 2 keys at once (open new tab)
Videos
Please see the solution that works with my example form
FormTab.html:
<!DOCTYPE html>
<html>
<body>
<form>
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<p>If you click "Submit", nothing happens.</p>
</body>
</html>
Java code:
WebDriver driver = new FirefoxDriver();
//Insert path to your file
driver.get("FormTab.html");
//Three example elements
WebElement firstField = driver.findElement(By.name("firstname"));
WebElement secondField = driver.findElement(By.name("lastname"));
WebElement submit = driver.findElement(By.name("submit"));
//Start with the first field
firstField.sendKeys();
//Verify that we in the first field
if(driver.switchTo().activeElement().equals(firstField))
System.out.println("First element is in a focus");
else
//Add Assertion here - stop execution
System.out.println("ASSERTION - first element not in the focus");
firstField.sendKeys(Keys.TAB);
//Verify that we in the second field
if(driver.switchTo().activeElement().equals(secondField))
System.out.println("Second element is in a focus");
else
//Add Assertion here - stop execution
System.out.println("ASSERTION - second element not in the focus");
secondField.sendKeys(Keys.TAB);
if(driver.switchTo().activeElement().equals(submit))
System.out.println("Submit element is in a focus");
else
//Add Assertion here - stop execution
System.out.println("ASSERTION - submit element not in the focus");
//Click the button
submit.click();
//Need be closed also in case the assertion - use @After
driver.close();
Try the below code.This is working fine.
Actions builder = new Actions(driver);
builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();
builder.Release().Perform();
builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();
builder.Release().Perform();
Use Action Chains:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
N = 5 # number of times you want to press TAB
actions = ActionChains(browser)
for _ in range(N):
actions = actions.send_keys(Keys.TAB)
actions.perform()
Or, since this is Python, you can even do:
actions = ActionChains(browser)
actions.send_keys(Keys.TAB * N)
actions.perform()
I think you can also write
uname.send_keys(Keys.TAB + Keys.TAB + Keys.TAB + ... )
It may be useful if you have only two or three commands to send.
In Ruby user1316's code looks like
driver.action.send_keys(elementVisible, :tab).send_keys(elementVisible, :return).perform
Keeping in mind the excerpt :
I can use tab key to get the button as
@element.send_keys :tab
@element --> any javascript element visible in the browser
but how do i use the enter key on the button??
In order to use the enter key on the button, you could try one of the solution provided using Ruby here. This basically talks about sending the :return value and not the :enter value i.e @element.send_keys :return and some additional information.
UPDATED:
I could provide some code in Java which tries to implement the problem conceptually using the info provided here. You could try to translate for the corresponding Ruby Selenium API.
The Code:
Actions builder = new Actions(driver);
builder.sendKeys( elementVisible, Keys.TAB).sendKeys(Keys.RETURN);
Action submitTheTransperentButton = builder.build();
submitTheTransperentButton.perform();
Hi,
I'm trying to find a way to send crtl + t.
I want to open a new tab and later switch between tabs.
I only find information for C#, but I think the way to write it is different in powershell.
Unfortunately I can't figure it out.
This is what I found for C#
//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");I have found how to send one key with powershell, but not 2.
This is working for one Tab press inside Google.com with powershell:
#load the web driver dll
Add-Type -path "C:\Program Files\WindowsPowerShell\Modules\Selenium\1.0\assemblies\WebDriver.dll"
#initiate a driver
$script:driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
#specify an implicit wait interval
$null = $driver.Manage().Timeouts().ImplicitlyWait((New-TimeSpan -Seconds 5))
#browse to a website
$driver.Url = 'https://www.google.com/'
$tab = $driver.FindElementByXPath('/html[1]/body[1]/div[1]/div[3]/form[1]/div[2]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/input[1]')
$tab.SendKeys([OpenQA.Selenium.Keys]::tab)There don't seem to be many resources for using selenium with powershell.
Any help is appreciated!