How to send KeyStroke in selenium to browser window (without locator) - java

Can i send KeyStroke in Selenium in Java without using a locator?
I want to send KeyStroke to WebBrowser itself, because I don't know element (and its locator), to whom I must send KeyStroke to perform action, which I want. But I know, that action performing correctly, when I manually select browser as active window and just press 'Enter' on the keyboard without selecting any element on the page.
I tried this code
SeleniumSession.keyPressNative(Integer.toString(KeyEvent.VK_ENTER));
but it didn't work for me.

What about sending it to the HTML element - ie find element by xpath "/html" and sendKeys() to it?

I'd try "//body", but I'm not sure it will work in the Selenium RC API. This is one of the things that Selenium 2.x's WebDriver API was designed to make work well.

Related

Selenium Webdriver (Java) , Need to send "Space" keypress to the website as whole

My problem is as follows.
I am attempting to automate part of a test suite for a website I work in, and while most of it went well, I'm at the point where the developers added a lightbox to confirm the next action.
Using firebug I found out an xpath I could use to click the button I need to proceed, but sadly it isn't working.
After some manual attempts, I figured that pressing the "Space" key, I can proceed.
The problem is that any sort of try using "driver.findElement" be it by xpath, or link text, fails with a "No such element" error in console.
So I'm trying to just send the keypress of Space , without using find element.
To be clear, I want to emulate someone just pressing space without clicking or selecting anything beforehand.
I tried with driver.Keyboard... but "Keyboard" isn't recognized, so I'm at a loss of how to send this keypress without using driver.findElement.
The piece of code giving me problems is:
driver.findElement(By.xpath("//div[4]/div[3]/div/button")).click();
Any help would be appreciated.
Thank you and have a great day!
If you receive NoSuchElementException, but you know that the element is there, then it seems that the element get loaded into the DOM later (with ajax?), than the page get loaded.
In this case you should use Implicit or Explicit Wait to wait until an element present, or an element is visible, etc...
If it still doesn't work, and you want to try the Space Key thing, then you can perform it on any element, for example on the <body> tag:
WebElement body = driver.findElement(By.tagName("body"));
body.sendKeys(Keys.SPACE);
Hope it helps.
try this:
Actions action = new Actions(Driver);
action.SendKeys(Keys.Space).Build().Perform();

Appium Finding Wrong Element when Finding With xPath

I am working on a development script using Selenium and Appium and I'm running into the issue of the wrong element being picked up by the Selenium Locator.
Essentially, I want to click a button that has no ID assigned to it; so the only thing I have left to identify it by is its text.
public Element button1(){
By locator = By.xpath("//android.widget.TextView[#text='button1']");
return new xElement(driver.findElement(locator), locator);
}
This is my my locator method to get the button1 object. By the way, no other button on the screen has text anywhere close to button1's text. The method click called on the button has the format:
public void clickBtn1(){
button1().click();
}
The button being clicked essentially has the text "wheelbarrow". This is just to clarify that the button being pressed has text no where close to button1's value.
I have used UI automator multiple times to confirm button1's actual text value. The weird thing is the script works occasionally, so I'm not sure what the issue is.
I have also tried a "wait for enabled" method to account for race conditions.
Try using the Appium inspector to search for your button. You can type in the xpath and search for the element to see what it finds. The other nice thing about the inspector is you can see how the native control attributes map to the Appium attributes. 'text' may not be the attribute you actually want. Also, have you tried searching on properties on the Button itself (instead of TextView)?
If it's working occasional, First try to use different element other than xpath. Second, try to give some sleep command before you perform that action like
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(ByLocator(locator)));

Why don't keyboard keys work in Selenium WebDriver?

I wanted to select an element through keyboard keys in Firefox. I am using this statement
driver.findElement(By.xpath("Element')]")).sendKeys(Keys.ARROW_DOWN);
But when I run it, I couldn't see any movement in the page. I am using JAVA to automate. This issue occurs even in BEHAT\MINK Tool.
My doubt:
is this feature not working because of the developers code?
or I need to modify my code to make it work?
For scrolling page up/down, you can send keys on body tag element of the page, like following:
driver.findElement(By.tagName("body")).sendKeys(Keys.UP); //to scroll up
driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN); //to scroll down

Java selenium. disable auto scrolling of the page

I'm using selenium of Java for automation tests and the browser is Firefox.
This is my sample code:
WebElement elem = driver.findElement(By.xpath(".//*[#id='main']/div/div[3]/div[1]/div/div[3]/div/div/div/a"));
Actions action = new Actions(driver);
Actions action2 = action.moveToElement(elem);
action2.perform();
The problem is that "moveToElement" action is triggering auto scrolling event of the page.
I want the page to remain as it was before without the scrolling.
somebody may know how can I disable this auto scrolling?
Thanks.
You can't. WebDriver scrolls elements into view when acting on them.
You can't disable the autoscroll.
Some possible workarounds:
You could try to fire a syntetic mouseover event over your WebElement if that would help your cause.
After the moveToElement(), you could try rescroll to your needed position with window.scrollTo() or for example the Page Up key. This would, obviously, break the mouseover on the element, but maybe it's what you need.
You can position your real mouse cursor over the element via the Robot class. This might get a little tricky, you might need to enter the fullscreen mode with your browser (or use this) and then handle the scroll offsets manually, if there are some.
All depends on your intentions, on what you really need to do with the element and why.

Selenium: Why does click() not behave like it does in firefox?

I have a link with an id:
<a href="#" onclick="return false();" id="lol">
In my test:
selenium.click("lol"); //when running the test, the click will just end up selecting the item, not firing off the frameworks javascript
This does not register the click the same! There is javascript that is part of a complex framework which will cause a div to popup. This works in firefox.
But this does work as a fix:
selenium.click("lol"); //when running the test, the click will just end up selecting the item, not firing off the frameworks javascript
selenium.keyPress("lol", "\\13"); //press enter key, since the click ended up selecting it
The fix does work. However, what is going on here? It seems that selenium.click() != [actual browser click event]. Can anyone help shed some light on these inner workings?
Selenium sometimes does not simulate click on javascript hrefs exactly. Maybe its the same issue here. A quick fix is to use a combination of selenium's mousedown and mouseup events. You can also consider using selenium.fireEvent("lol","click");. Revert back when you have tried these.
It is possible to click a link in a browser before the javascript is loaded. See this other question. One solution would be to wait for some element to be visible on the page that is put there by javascript.

Categories

Resources