Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a working Selenium WebDriver python-script from Selenium IDE?

I am new to selenium, and I am trying to use Selenium IDE (2.9.0) to create a first click-and-record script as a basic, which I then refine with Selenium WebDriver (2.48.0).

I recorded a working script (see attached at the end of this question), and exported it as "python 2 / unittest / WebDriver". However, the source code made it clear, that there are some problems with it (Commented lines with discomforting "ERROR" statements):

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Test1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://ironspider.ca/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_1(self):
        driver = self.driver
        driver.get(self.base_url + "/frames/frames_example1/advanced.htm")
        # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | content | ]]
        self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text)
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=menu | ]]
        driver.find_element_by_link_text("Chapter 2").click()
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=content | ]]
        self.assertEqual("The Falling Star", driver.find_element_by_css_selector("h2").text)

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Running this code also did not work. The error is:

ERROR: test_1 (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Privat\Learn\Selenium\test1.py", line 22, in test_1
    self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 402, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"h2"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/dial1/AppData/Local/Temp/tmpu1otxnnn/extensions/[email protected]/components/driver-component.js:10659)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/dial1/AppData/Local/Temp/tmpu1otxnnn/extensions/[email protected]/components/driver-component.js:621)

----------------------------------------------------------------------
Ran 1 test in 34.166s

FAILED (errors=1)

Do I have to manually fix all the errors (if so, how?)? Can't "Selenium IDE" export a working version of the script for the WebDriver? Do I need to install something else? Is my general approach completely wrong? Maybe I should use something else than Selenium?

Here is the original working Selenium IDE Testcase Script.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://ironspider.ca/" />
<title>example1</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">example1</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/frames/frames_example1/advanced.htm</td>
    <td></td>
</tr>
<tr>
    <td>selectFrame</td>
    <td>content</td>
    <td></td>
</tr>
<tr>
    <td>assertText</td>
    <td>css=h2</td>
    <td>The Eve of the War</td>
</tr>
<tr>
    <td>selectWindow</td>
    <td>name=menu</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>link=Chapter 2</td>
    <td></td>
</tr>
<tr>
    <td>selectWindow</td>
    <td>name=content</td>
    <td></td>
</tr>
<tr>
    <td>pause</td>
    <td>500</td>
    <td></td>
</tr>
<tr>
    <td>assertText</td>
    <td>css=h2</td>
    <td>The Falling Star</td>
</tr>

</tbody></table>
</body>
</html>
like image 596
Alex Avatar asked Nov 09 '22 03:11

Alex


1 Answers

Unfortunately Selenium IDE is not a perfect tool for automation testing, only as a start for beginners, that's why huge part of code it generates may contain outdated or just wrong code (wrong commands in your case). You will need to handle each of those exceptions manually.

I know that it looks lame, but take a look at documentation Selenium Python

It will help you to rewrite those methods in proper form. Hope this will hepl you

like image 63
Dmitry Malinovsky Avatar answered Nov 14 '22 23:11

Dmitry Malinovsky