Hot answers tagged

27

driver.close() and driver.quit() are two different methods for closing the browser session in Selenium WebDriver. driver.close() - It closes the the browser window on which the focus is set. driver.quit() – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully. You should use ...


17

All the learning starts from step1. You have learned based on your own interest. I had a lot of support from this community and StackOverflow when I started writing automated tools. I can share my experience. 5 Rules for beginners getting into test automation: Rule#1 - Do not worry about design, error handling when you start writing your automation suite. ...


13

Not Working I've tried the --lang=us option, as suggested, but also had no success using the code below. My webpages were still being downloaded with my default language (that's not en). from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('lang=en') # options.add_argument('--lang=en') <- Tried this option as well ...


11

I've tested using format http://user:pass@host and it works. So in Python (in setUp() of MyClass(unittest.TestCase) class) this should look like: self.base_url = "http://user:pass@host" In Java based on #34 at code.google, the following code should work as well: public void login(String username, String password){ WebDriver driver = getDriver(); ...


9

I have worked with Web Driver in both java and C# and I use In Java : WebDriver driver; driver.quit(); In C# : IWebDriver Driver; Driver.Quit();


9

If your driver implements WebStorage you can invoke driver.getSessionStorage().clear(); driver.getLocalStorage().clear(); In order to make sure that the driver does implement WebStorage the idiom is if (driver instanceof WebStorage) { WebStorage webStorage = (WebStorage)driver; webStorage.getSessionStorage().clear(); webStorage.getLocalStorage(...


8

Similar to Will's answer, but finds the <select> by its element name, and clicks based on the <option> text. from selenium import webdriver b = webdriver.Firefox() b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()


8

I haven't used the Python bindings, but as far as I know they should be equivalent to the Java ones. If I were you I would try to find the Python equivalents of findElements() and isDisplayed() that are available in the Java bindings. For example, I would do something similar to this: // ... myElementList = driver.findElements(By.Id("fancybox-close")) if ...


8

checkboxes = self.browser.find_elements_by_xpath("//input[@name='arr[]']") for checkbox in checkboxes: if not checkbox.isSelected(): checkbox.click() (Previous answer): I am not yet familiar with the python syntax, but this is what you can do: Return all elements with the given xpath: self.browser.find_elements_by_xpath("//input[@name='arr[]']...


8

I'm going with my favorite response here: it depends. Sometimes the decision is made because that's the language the tool supports. Sometimes the language is a flavor of the language used by the development team - this often happens where there's an expectation that the development team will be writing at least some of the test automation code. Sometimes ...


8

The installation instructions referenced by @dimitry-cheremushkin have been changed and recommend using pyenv instead. Tox and Pyenv can be used together like this: First we ensure we have pyenv installed: $ brew update $ brew install pyenv Then we install all the needed python versions with pyenv. This might take time. See python.org/downloads for ...


7

My experience with automation is that it's invaluable for regression, particularly the kinds of regression that are tedious and painstaking to perform. A login script is usually a utility that happens as part of a larger script suite - which must, as Siva said, be object-oriented and data-driven if you don't want to create yourself a maintenance nightmare. ...


7

Full disclosure: we rely heavily on FitNesse, run many hours worth of FitNesse tests daily in our CI buildbot daily, and I personally wrote both FitNesse tests (using Fit wiki format in browser) and WebDriver tests in Python. FitNesse is for end-to-end testing (browser automation) and Selenium/WebDriver in much superior replacement, especially if you ...


7

You can use Explicit Wait: An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. OPTION-1: driver.implicitly_wait(10) # seconds select_country = Select(self.browser.css("#country_id")) select_country.select_by_visible_text("Russia") city = driver.find_element_by_css_selector("#city_id") ...


6

The test is slow not due to slow exception handling, but because driver waits for elements thats are not found. In this case driver waits for the pop-up - maybe it will appear. Try to use self.driver.implicitly_wait(0) This will tell driver not to wait if it cannot find some elements.


6

You may need to use .get_attribute("value"). instead of .text.


6

The answer Peter Masiar gave no longer works. This answer will result in the following exception: Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in get self.execute(Command.GET, {'url': url}) ...


6

Oh Boy! you might start another one of the Java vs Python vs Other language war. These are kind of question which are always answered with preference for a specific language in mind. For me- I would prefer Python over Java, because of one reason : Its simplicity. Our test team comprises of people who have little programming experience. However, most of ...


6

Nose is no longer maintained: Nose has been in maintenance mode for the past several years and will likely cease without a new person/team to take over maintainership. New projects should consider using Nose2, py.test, or just plain unittest/unittest2. Even though unittest from the Python standard library is a very mature test framework with built-...


6

https://pypi.python.org/pypi/LinkChecker - Python module to build link checkers. Dev status is Production/Stable. There are dozens of other tools like this, in many languages. It makes perfect sense to use open-source debugged version of such utility instead of reinventing the wheel. BTW I got the answer by googling "check broken links python" :-)


6

I think in Python you can check for None or "" on Empty string Some options: self.assertEqual(element.text, None) self.assertEqual(element.text, "") self.assertEqual(len(element.text), 0) Also check this question for other options: https://stackoverflow.com/questions/9573244/how-to-check-if-the-string-is-empty


6

You can either: Look for all element that match certain search criteria: if driver.find_elements_by_css_selector(my_locator): # No element found here Catch an exception when searching for a single instance: try: webdriver.find_element_by_css_selector(my_locator) except NoSuchElementException: # No element found here


5

Do you need Selenium for this task? If you have an existing suite of Selenium tests, you can run them against the new site to verify the functionality is working, but in general Selenium is not the best tool for validating look and feel, or massive amounts of content. There are several link checker tools that will crawl all of the links and download all ...


5

Since you are looking into Selenium (as a test framework) start with Java, because Googling on "Selenium/Webdriver issue description" will often result in Java examples that you can re-use. There is just more people doing Selenium with Java then Python. Also Selenium it-self is mainly written in Java. For a project I would learn the language the application ...


5

As @Niels van Reijmersdal mentioned, it makes sense to use the language in which you have local experts available. But you don't have to use the same language - use the language in which your team will be more productive. Python and Ruby are almost identical in expression power. I would argue that Python is more readable, which is significant advantage when ...


5

The problem is that you don't switch to opened window, and webdriver searches for elements in the old page instead of the newly opened one. Solution: String winHandleBefore = driver.getWindowHandle(); for(String winHandle : driver.getWindowHandles()){ driver.switchTo().window(winHandle); } This java method's part switches to ...


5

The reason the first two don't work is because you didn't include the full class value for either the form or div element in your xpath. Try //form[@class='ng-untouched ng-pristine ng-valid']/div[2]/div/label OR //form[contains(@class,'ng-untouched')]/div[2]/div/label Note the use of contains() to match against a partial value. I've found this to be ...


5

I think you just need to re-raise the assertion error after printing out log messages: try: self.assertEqual(len(self.verificationErrors), 0) # no errors except AssertionError as e: for message in self.verificationErrors: print(str(message)) raise # < HERE Also, if you want to check for severe console messages after every single ...


5

Where is the best place to store a large number of Selenium tests? The ideal place for your selenium tests would be a source code repository, like git or mercurial. You don't have to host the repository yourself and can use third-party services, code hosting solutions like github or bitbucket. And are there any tools which I can use to manage these ...


4

I am the author. Actually this is a patch-like binding. You should know what I said in step 2. You need to download the source code of official Python bindings for Selenium and then copy my patch to the source files and install the patch source code, as mentioned in step 3 and 4.


Only top voted, non community-wiki answers of a minimum length are eligible