Friday, February 22, 2019

py0015: NameError: name 'Select' is not defined

Error 
select = Select(driver.find_element_by_id('id****')) 
NameError: name 'Select' is not defined

This error i received when i tried to select value from drop down menu while doing automation in python selenium

How ever i came to know that i was using correct code but some import class is missing from header of file. I need to add "from selenium.webdriver.support.select import Select" in main file or same file so that code will work fine.

Solution:
from selenium.webdriver.support.select import Select

Correct code for click on drop down:
select = Select(driver.find_element_by_id('**id**'))
select.select_by_visible_text('String')

Thursday, February 21, 2019

py0014: table_name1 = table_name.replace(',','') AttributeError: 'pyodbc.Row' object has no attribute 'replace'

Problem :
below is my sample code, where i am getting error table_name1 = table_name.replace(',','') AttributeError: 'pyodbc.Row' object has no attribute 'replace' and in same example you can cover how to use replace in python
    cursor.execute('select table_name from information_schema.tables')
    for table_name in cursor:
        table_name1 = table_name.replace(',','')
        print (table_name1)

I was trying to loop the list of all table names which are in schema using query "select table_name from information_schema.tables". The intention was to do some action based on the table exists or not.

Why i used replace:
suppose after for loop when i use command "print (table_name)", it gives output enclosed in single quotes and postfix by comma, so i used replace function to replace comma and it starts throwing above error.

Solution:
if you notice code above i gave "table_name" in for loop and for replace function we have to write it as str.replace(old,new). But in my case replace was not able to get any string in "table_name" because "table_name" is just act as "i" here in for loop and it is not a attribute in this situation.

So i have to use "table_name.table_name.replace(',','')"

Correct code as below:
    cursor.execute('select table_name from intermediate_table')
    for tb in cursor:

        table_name1 = tb.table_name.replace(',','')
        print (table_name1)

Here i have replace table_name in for loop with "tb" for better visibilty and better understanding.

Monday, February 18, 2019

py0013 : use function from another file in main file python

To make code convient and readable form it is neccessary to break code in multiple files, where you can write your code and write function can be later call on any where in the project. In python its easy to do.

Below is the sample code, i am taking the example from my previous post

py0012 : try catch in Python/ DB connection in try catch

which is having file name "file1" and t funcation made which will return something on db connection pass or fail and that can be used in mail file as per below code example. First we need to import file, then use funcation name directly "t()" without any file reference.


from file1 import *
if t() == "DBConnectionError":
    # your code will stop here, dependent file will not throw any error
exit(0)


if t() == 1:
# if database connection pass it will move further as per the written commands, this also show that how we can handle error on other files call
    driver = selenium.webdriver.Firefox(executable_path=FirefoxDriver, firefox_binary=binary, firefox_profile=profile)

py0012 : try catch in Python / DB connection in try catch

try catch is preliminary to add in your code, other wise if it break in between it looks ugly when error come to end user. To make code clean and show user friendly error you can use try catch, see one of the example below when database connection.

Earlier when database connection fails, it show red color unexpected error and since this file is being in use further so need to use try catch which will return something so that further can be used as if else for any type of condition

import os
import pyodbc
from configuration import *

try:
    crsrconn = pyodbc .connect("Driver={SQL Server Native Client 11.0};"
                                "Server="+ServerName+";"
                                "Database="+DatabaseName+";"
                                "Trusted_Connection=yes;"
                                "pwd="+Password+";")
    cursor = crsrconn.cursor()
    cursor.execute('SELECT count(*) rn FROM *****')
    for row in cursor:

        if row != 0:
            def t(): return 1
            print ('1')
        #else:
         #   def t(): return 0
          #  print ('0')

except pyodbc.Error as err:
    print("DBConnectionError")
    def t(): return ("DBConnectionError")

py0011 : Learnt Something impressive in Python

Code writing techniques

Today i tried to write some code in python like i used for , if , else, try, catch, return. By using this came to know python has some limitation, no we cant say limitation its syntax.


  • For "if" we can not keep the code below on same starting column point, you have to give "tab" space in front of them for any hierarchy .
  • write "return" like this, def t() return 1 or def t() return "true"
web stats