How to use Sendkeys() without finding element on web page when element is already focused and waiting for input

南笙酒味 提交于 2020-01-04 01:52:12

问题


I'm trying to write a simple Java based selenium code where I would load a page, give the desired values to username & password and login to web page.

Now once the web page loads it automatically waits for user to enter username i.e username is already focused. So can I send the keys to this already focused element. Once I have given the input to username I could use TAB to select the next input i.e. password and then TAB again to select the Login button.


回答1:


Try this - WebElement currentElement = driver.switchTo().activeElement();

Refer to this for more details - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html#activeElement--




回答2:


Alternatively you can use the ROBOT function as a workaround. For example: to send 123456 you may use

Robot robot = new Robot();      
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
robot.keyPress(KeyEvent.VK_3);
robot.keyRelease(KeyEvent.VK_3);
robot.keyPress(KeyEvent.VK_4);
robot.keyRelease(KeyEvent.VK_4);
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
robot.keyPress(KeyEvent.VK_6);
robot.keyRelease(KeyEvent.VK_6);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

I have also used the Sikuli sendkeys feature in the past succesfully.




回答3:


if u want to use tab button to particular locator then

WebElement inputField = driver.findElement(By.Locator("LocatorValue"));
inputField.sendKeys(Keys.TAB);



回答4:


Use below code for the same :

driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);

I have tried the demo and its fine

public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
        Thread.sleep(1000);
        driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
        Thread.sleep(1000);
        driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
        Thread.sleep(1000);
        driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
        Thread.sleep(1000);
        driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
        System.out.println("OK");
    }



回答5:


To navigate to the next component, you can use Class KeyboardFocusManager:

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();

To Learn more about this use link https://docs.oracle.com/javase/7/docs/api/java/awt/KeyboardFocusManager.html




回答6:


No you shouldn't use Sendkeys() without finding element.

As you mentioned once the web page loads it automatically waits for user to enter username i.e username is already focused which resembles to the username field to be in focus and is based on HTMLElement.focus().

HTMLElement.focus()

The HTMLElement.focus() method sets focus on the specified element, if it can be focused.

But for Selenium to perform it's task the Browser Client needs to be in focus which resembles to the Browser Window to be in focus and is based on Window focus() method.

Window focus()

The Window focus() method sets focus to the current window.

Now from the above two discussion, it is pretty evident that username field being in focus wouldn't help Selenium to interact with WebElements.

sendKeys()

sendKeys() method is used to simulate typing into an element, which may set its value. Now to set its value of-coarse you have to find/locate the element/elements either one of the following methods :

  • findElement()
  • findElements()

Why not to use TAB to select the next input

It's possible to use sendKeys(Keys.TAB); inconjunction with WebElement myElem = driver.switchTo().activeElement(); to switch to the element that currently has the focus within the document through switchTo() or the body element if this cannot be detected. This exactly matches the semantics of calling document.activeElement in Javascript. But there is a pitfall. If the tabindex is improper your script may Fail without providing you any other option but to fall back on findElement().

Conclusion

From the above mentioned points findElement() inconjunction with sendKeys() is always a safe bet.



来源:https://stackoverflow.com/questions/50286111/how-to-use-sendkeys-without-finding-element-on-web-page-when-element-is-alread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!