Selenium Silly GetAttribute Issue. Debug vs running code java - selenium

I'm using Eclipse/Java and interfacing with the Selenium chrome webdriver.
The code I'm executing is
Sting sValue = item.getAttribute("innerHTML");
If I am stepping over this code it works fine.
Otherwise, if I just run the code. It throws a NUllPointerException.
What gives? Any ideas?
Thanks!

It's possible the page doesn't have enough time to load when you run straight through, but when you step with the debugger it's just enough extra time for the page to finish loading and avoid the NullPointerException. Try adding a wait, as described in the accepted answer for this question: Getting Selenium to pause for X seconds.

This is not a real answer, it's just a total hack! I have to get some things done so I'll investigate predicates and WebDriverWaits later.
I was creating a list of webelements:
List wElements = getElements()
Then I was trying to get the innerHTML from each webelement in the list.
It works fine in debug mode, but when I execute the code it fails to gather the information IN SOME OF THE ELEMENTS in the collection. So what I did was retrieve the same list a second time.
List wElementsB = getElements()
Then I retrieved the innerHTML out of the wELementsB. Works fine.
I tried sleeping and telling the webdriver to wait. but those failed as well.
Thanks Guys!

Related

nightwatch test fails with multiple click events in sequence

When running a nightwatch.js test on a browser with a checkbox that
is not visible using the following code.
browser.moveToElement('input[id="2verificationYes"]')
.click('input[id="2verificationYes"]')
I get the following error :
An error occurred while running .click() command on ,input[id="2verificationYes"]>: unknown error: Element `element` is not clickable at point (111, 701). Other element would receive the click: `otherElement`
at process._tickCallback (internal/process/next_tick.js:64:7)
I am using
nightwatch v.1.0.19, chromedriver ^2.43.0, geckodriver ^1.16.2, selenium-server ^3.14.0
I have tried using the callback functions with each call but the result is the same. Tried to research how to scroll to an element in nightwatch, but the api's don't have that. It is my understanding the the moveToElement function is supposed to scroll to the element.
For me moveToElement is working good. I believe you are seeing error because you forgot to mentioned the Xoffset and Yoffset.
Try my below work around and let me know:
browser.moveToElement('input[id="2verificationYes"]',Xoffset,Yoffset)
.click('input[id="2verificationYes"]')
for your reference: search 'moveToElement' here
Can you try giving '0' and let me know:
browser.moveToElement('input[id="2verificationYes"]',0,0)
.click('input[id="2verificationYes"]')
Same time try giving little wait time to narrow down the issue. Good luck

When invoking js.exec in Geb/Spock, the exec method is flagged as 'null'

I am creating a suite of tests (using Geb/Spock) for a web site. In one of them, the element I want to access is on the top of the page, so, to make sure that is visible, I want to scroll to the top of the page.
The command I am using is:
browser.js.exec('window.scrollTo(0, 0);')
or variations of it like
js.exec('window.scrollTo(0, 0);')
or other alternative like:
js.exec('window.scrollBy(0, -250);')
None of them makes the page scroll up, and when executing I get the following error (it is the only error, no other feedback). The error message using the other options listed above is identical (other than the command itself):
Condition not satisfied:
browser.js.exec('window.scrollTo(0, 0);')
| | |
| | null
| geb.js.JavascriptInterface#4019094f
geb.Browser#3dcac33e
at UserCreatesCompany.Go to Home Page and click on the log to
GitHub button as user User1(UserCreatesCompany.groovy:170)
I can not interpret the message that 'exec' is null. What exactly it means?
To make things more interesting, at the end of this script I am running the following cleanup procedure
js.exec('window.scrollTo(0, document.body.scrollHeight);')
DeleteButton.click()
$("button",'data-automation-id':"button-modal-yes").click()
}
and that works well: the page scrolls down. So, does not seem a problem about some missing library.
Any suggestion of what I may be doing wrong?
The version of the different components I am using is:
groovyVersion = '2.5.4'
gebVersion = '2.3'
seleniumVersion = '3.141.59'
chromeDriverVersion = '2.45'
First of all, you should not need to ever manually scroll the page to make elements visible - Selenium WebDriver which is underpinning Geb will do that for you automatically as soon as you start interacting (clicking, setting value, etc) with content.
Secondly, the failure you are getting is a failed assertion coming from a statement in an automatically asserted (then: or expect:) Spock block. It feels to me that you don't understand a concept which is core to Spock and therefore you should read about it in the manual first. It should make the failure you're getting clearer.
Thanks for the answer. Clearly: I was not fully aware of the different constrains the different blocks impose on what is executable or not. The manual is pretty clear once you have stumbled!
I am intrigued by your first assertion pointing that Selenium WebDriver will move to the element as soon as I interact with it. That was my understanding but it was not working. I made sure the element in question had a unique identifier, but still, it was not able to found it if the element had to be found by scrolling up. On the other hand it worked smoothly when locating the element WebDriver scrolled the page down.
Thanks again for the explanation. I have learn something new today!

Instruments fails to execute send_keys intermittently. How should that be handled?

The Problem: I'm using Appium's python client to send_keys to a WebElement.
And Instruments decides it shouldn't tap certain keys sometimes.
This is a pain when I'm trying to do things like login. I need to be able to reliably type or programmatically set values on input fields.
This is (more or less) what I'm doing, and here's the gist of Instruments complaining about it:
el = driver.find_element_by_xpath('//UIATextField[1]')
el.click()
el.send_keys('ABCDEFGHI')
All pretty standard. And it usually works
But usually isn't cutting it. I need something solid.
Possible Solutions?
I think I can make send_keys work if I do some sort of try/retry if I get a WebDriverException back.
But what would be really cool is if I could set the value of the element through a JavaScript execute_script - or better yet - a selenium python binding!
I don't know JavaScript, and I've already tried searching for how to set the value on the object without doing sendKeys but I've come up blank.
Any ideas would be really helpful. Thanks!
Solution below:
# Get the element
el = driver.find_element_by_class_name('UIATextField')
driver.execute_script("au.getElement('%s').setValue('%s')" % (el.id, 'ABCDEF'))
# At this point, the keyboard is opened and the text is instantly entered.

In Selenium, does ClickAndHold work with webdriver and chrome?

I'm trying to make a click-and-drag event work using Selenium Webdriver (2.15). It works fine when using FF, but in Chrome it does not. In Chrome, it appears to have no effect. Here's what my code looks like:
Actions builder = new Actions(GuiOps.driver);
builder.MoveToElement(fromElem).ClickAndHold().MoveToElement(toElem).Release().Build().Perform();
Has anyone gotten ClickAndHold() to work successfully using Chrome?
Turns out this is a known issue. There's a workaround posted at http://code.google.com/p/chromium/issues/detail?id=92312 . Basically, you add a MoveByOffset(1,1) immediately after that ClickAndHold() call and it works.
First post/comment here. :)
Well, it worked for me. All you need to give is a bit of a pause after the "moveByOffset(1, 1)". It seems that the script is executing faster than it should. Here's a piece of code that worked for me:
"actions.clickAndHold(we).moveByOffset(1, 1).pause(1000).perform();". Hope it helps.

Selenium: intermittent "element not found" issues

Every now and then my selenium tests randomly fail with an "element X not found" error message. I do a simple select by ID, eg.
click('sideBar_queryButton')
When I use the dom inspector, the element is there, so I wonder why selenium doesn't find it sometimes. When I run the same test again, it works or it breaks again, there seems to be no reliable way of reproducing it. Even tests which are there for ages seem to randomly break and then magically work again. Inserting a few sleep statements sometimes helps but not reliably. So I wonder if I'm using it incorrectly. Has anyone else had these problems with selenium and if so, how did you fix them?
Edit: I found it to be much more reliable to put some test markers in my pages and wait for them to appear. If you use asynchronous operations which might create race conditions in your tests, inserting a test marker into your html after you finish the operation worked pretty well for me. E.g.
$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));
That way, you can do a simple "waitForTextPresent" to see if things worked out and its much more reliable than guessing the browser's loading state. The testmarker class needs to be formatted in a way that it is not visible to the user (e.g. font color == background color).
Thanks for all your comments. After some deeper digging on the net and
in our tests I finally found combining these statements instead of a
simple waitForPageToLoad to be the cure for our issues:
waitForPageToLoad('')
// wait until all ajax activity has ceased. That check's jQuery's $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// wait a second for all JS to properly initialize
pause(1000)
There is still a pause in there which is somewhat ugly, but it does
the trick.