Selenium 2.0rc3 click function too fast? - python

First off, sorry for the cryptic question.
My team is currently using Selenium 2.0rc3 (with python) for testing our web app with chrome. When we used the 2.02b version of Selenium, our test passed (it was a little slow and we had small hacks that we added to webdriver). After we upgraded, the test became extremely fast and started failing. After debugging we found out most test failed because webdrivers click() function was not blocking() successive calls. Currently we added a sleep()/timeout of .5 secs after each click and while this solves the immediate problem, it doesn't quite achieve our main goal (which is to speed up our test)

Your problem is not really that it's clicking too fast. Just that it's clicking before that element is present. There are two ways to get round this:
Wait until the element is present before clicking
Increase the implicit wait time
I'm afraid I haven't used the WebDriver Python bindings. However, I can tell you how it is done in Java and hopefully you can find the Python equivalent yourself.
To wait for an element, we have in Java a class called WebDriverWait. You would write a Function which you pass to the until() method which passes only when the element exists. One way you could do that is with driver.findElements( By... ) or wrap driver.findElement( By... ) in an exception handler. The Function is polled till it returns true or the timeout specified is hit.
The second method is the preferred method for your case and in Java you can do driver.manage().timeouts().implicitlyWait( ... ).

I've tried the selenium-2 rc3 python bindings for chrome. My experience was the opposite of what you're describing - after clicking, the driver didn't know that the page was ready for it to continue. So instead of speeding up the tests, they turned out very slow (because the driver was waiting for ages). However, the firefox driver seems pretty stable - maybe you should stick with it until the chrome driver gets baked a bit more.

If the click() executes ajax calls I would suggest you to use NicelyResynchronizingAjaxController

Related

Checking if a function from the sources of a website is executed

With the "Inspect Element" option in a common browser it is possible to access the "Sources" tab and, not only see the files which the website uses, but also mark a line of code (as shown in the image below at line 463 with a .js file), which will make the browser pause when that line of code is executed (essentially a debugger). In this sense, it seems possible to check if a certain line of code is executed, which is what I need to finish an automation with Python, preferably with Selenium, but which I also don't know how to do.
Selenium tests are intended to be "black box". That is, you load a page and only access the things that are available in the browser window to verify that behavior is correct. You should NOT try to verify that specific parts of code were written. Even if you can figure out a way to do this, it will make your tests extremely brittle.
If you are writing the JavaScript code, then I suggest using a framework such as Jest or Mocha to test it directly. You still shouldn't verify that a specific line of code is executed, but you can test a function by calling it directly and checking the return value or side effects to make sure they are correct.

Selenium "sendkeys" does not work with a particular website but manually typing it in works

https://phytozome-next.jgi.doe.gov/blast-search
on this website if i use the blast tool with the sequence "MARPFTLSLLSLCLLLSAWSCFGGSSSTNRFNICQLNSLNALKPDHRVETDGGLVETWSSRHPELECAGVTVTRRTLYRNGFQMPSYSPYSQMIMAIQGKGALGLALSGCAETYEEPAKESSSSSQKPSDSHQKIRQFDQGHVMLIPRGVPFWIFNTGDEPLITVTLLDTSSEDNQLDQSPREFYLAGNPDIEHPEAMKEKQQQQAEEEGGNVLSGFGKRFLARALNIDQDTANKLISPDDEMKQIVKLKEGLSVISPKWQGQQEDEDEDDDDEDEDESVSRPSRRPSHGKRVHKEEETVVEPYPHGKHVHKEVEKEVEPLPPRKHVHKEEEKEIEPLPPRRSRHHHDEGEDEGEEEEKPRARRTRGPTPSPKGEGHRGVEEEDESEDTKGHKTRHEKTRHEKSWKEHRPEGEDVEKGEAHEEWETRPSKDKPHGSNGLDETICSSKLQFNIARPKGADFYNPKAGRIKNLNSQSLPALQHFGLSAQYVVLYKNGIYSPHWNMDANSVIYVIRGQGQVRVVNNEGIVMFDDELKKGQLLVVPQNFMVAEEAGDQGFEYVVFKTNDNAVTSYLKETFRAFPAEVLVNIYKLKHSQVHDLKYNGNLGPLVNPENSLDQSS"
along with Archaeplastida selected in order to return all results i get 100 results
when i use selenium with the chrome driver the web page informs me that "The Phytozome service couldn't get what you were after."
i have narrowed the problem down to something involving send keys, since if i run selenium all the way up to the point where it sends keys and i input the keys myself it works just fine.
When you say you "input the keys yourself", are you really typing those hundreds of random-looking letters yourself, one at a time? That looks about impossible.
It totally depends on the particular website's programming, but it could be a timing issue. You can try a separate SendKeys() call for each character with a pause between each one.
i am not sure what the problem was but i simply used execute script instead of send keys. that posed another issue, which i resolved by sending a space key and then clicking backspace.
i tried everything including sending the keys one by one with a space in between and this was the only thing that worked.

Python Selenium Passing Browser to Function

I have a thread creating a selenium browser with PhantomJS
Browser= webdriver.PhantomJS('C:\phantomjs.exe',desired_capabilities=dcap, service_args=service_args)
But I want to continue to loop over data on this page unless conditions are met. The easiest way I can think to do this is to set a separate function but passing the browser to the function doesn't seem to work like this:
DoTheThing(Browser)
The DoTheThing function doesn't seem to have any access to that browser. Is there a way to do this or will I have to keep recreating the browser in each function. Or maybe somehow make it global which wouldn't be neat but it'd work for this case.

Selenium - what is the difference between click and clickAndWait?

I am new to selenium and need some clarification on some stuff. I've tried click and clickAndWait in the IDE, and while the references stated clearly what each meant, when I export the test case to Python, it seems like both are doing the same thing here
driver.find_element_by_xpath("//li[#id='pa-u_8298348-bd']/a/span[2]").click() #click and wait
driver.find_element_by_link_text("IMVironments").click() #click
Can someone tell me what is the difference here then?
From Selenium docs:
Many Actions can be called with the "AndWait" suffix, e.g.
"clickAndWait". This suffix tells Selenium that the action will cause
the browser to make a call to the server, and that Selenium should
wait for a new page to load.
2 years later, I know. But I was looking for the same answer and found it.
From the Selenium Wiki
The AndWait alternative is always used when the action causes the browser to navigate to another page or reload the present one.
In other words, Selenium's webdriver, when utilizing the Click() command, will inherently use the "AndWait" modifier if it recognizes a page load.
See also Interface WebElement
void click()
Click this element. If this causes a new page to load, this method will attempt to block until the page has loaded.
I'm not sure if this is a python thing, but it appears you are only calling 'click' and are not calling 'clickAndWait'. I think this is what Trott is pointing out too.
With this said I have noticed that IE often requires AndWait a bit more often than Firefox or Chrome, which seem to deal with just click a bit better (due to speed?). You can also create your own 'wait' in Selenium, which is what I usually do in critical situations (as in you need to interact with a specific element so you 'wait' until it exists).
EDIT:
After your comment, I now understand what you are saying (was mildly confused about the 'clickandwait' comment :P). From what I can tell AndWait is not always available for the Python binding, I found that here, but I'll admit it's not 100% clear as it suggests otherwise in other places. Perhaps this would be useful instead?
wait_for_page_to_load(timeout)[source]
Waits for a new page to load.
You can use this command instead of the “AndWait” suffixes,
“clickAndWait”, “selectAndWait”, “typeAndWait” etc. (which are only
available in the JS API).
Placed this as an edit, because was too long for a comment, sorry!

How do the Selenium bindings work, and how do I use them?

Note: This may be more of a "meta" issue of how to understand documentation rather than a specific issue with the Selenium bindings themselves.
I'm looking at the Selenium Python bindings, and I'm a bit confused.
Most examples of how to start a simple instance of the browser require something like this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("www.google.com")
Now, I look at the webdriver.firefox object and I see that there are four methods associated with that object! I'm sure there should be more than this. Where, for instance, is .get() in my example?
So I start looking around and I see that there are a bunch of methods I can use on the webdriver.Firefox() object in webdriver.remote. But this doesn't seem to make sense? I'm not using webdriver.remote? Why are all these methods in here?
Finally, my confusion is compounded by the fact that there's selenium.selenium, which has a host of interesting and powerful methods, but I have no idea how to use them. Then don't work on the webdriver.Firefox() object, so how do I use them? Why do they never appear in any examples?
webdriver.Firefox() is in fact webdriver.firefox.WebDriver.
Just have a look on the code in selenium/webdriver/__init__.py and selenium/webdriver/firefox/webdriver.py, which inherits a lot from the RemoteWebDriver class.
There is a short introduction to the selenium python bindings, too. Have fun!

Categories