We can press TAB key or any other key in Selenium Web driver by 2 ways in Java.
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
- Using Keys.TAB
- Using Actions
Using Keys.TAB
To use this method, you will have to import below class.
import org.openqa.selenium.Keys;
driver.findElement(By.name("name")).sendKeys(Keys.TAB);
Using Actions Class
To use this method, you will have to import below classes.
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
Actions action = new Actions(driver);
action.sendKeys(Keys.TAB).build().perform();
Complete Example in Java
Complete Example in Java
package temp; import java.io.File; import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.Keys; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @SuppressWarnings("unused") public class SendKey { public static void main(String [] arg) { System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe"); WebDriver driver = new ChromeDriver(); try{ driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS); driver.get("https://www.google.co.in/preferences"); driver.manage().window().maximize(); //driver.get("http://register.rediff.com/register/register.php"); Thread.sleep(2000); WebElement e = driver.findElement(By.xpath("//div[@id='instant-radio']/div[3]/span")); //using Keys.TAB e.sendKeys(Keys.TAB); //using Actions Actions action = new Actions(driver); action.sendKeys(e,Keys.TAB).build().perform(); } catch(Exception e){ System.out.println("Exception - > " + e.toString()); } finally{ driver.close(); driver.quit(); } } //main function ends }//class ends
Please note that we can also press any other key like ENTER, F1, F2, HOME, ARROW_UP, ESCAPE in same fashion.
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
Too good explanation and very clean code ! Thank you !
ReplyDeleteReally good explanantion. Thanks!
ReplyDeleteHi Sagar,
ReplyDeleteCan you let me know how to handle the java applet window which says,
do you want to continue the connection to this website is untrusted in Internet explorer using webdriver java.
By default the cancel button is highlighted in applet window. I just want to click continue button.
Thanks in advance.
Hi Tamil,
DeleteYou can not handle Applet elements using Selenium.
You will have to use AutoIT or Ranorex or LeanFT for that.
I tried using Robot class to tab to continue button and click it. It works only couple of times but not consistent. I even used thread. sleep but I still face synchronization issues.
DeleteI tried using Autoit but it does not identify the class and instance. I guess autoit workd only for windows dialog not sure though.
Thanks
Can you try pressing key on Applet element. I do not know of any other solution!
Delete