4

I'm trying to retrieve data from the dynamic web table in selenium python but errors in a console as ERROR as "

values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+row+"]/td["+col+"]").text

TypeError: must be str, not int

FAILED (errors=1)

class DynamicWebTable1(unittest.TestCase):


@classmethod
def setUpClass(cls):
    chrome_driver_path = os.path.abspath('..')  + "\\Drivers\\chromedriver.exe"

    cls.driver=webdriver.Chrome(chrome_driver_path)

    cls.driver.implicitly_wait(30)
    cls.driver.maximize_window()
    # navigate to the application home page
    cls.driver.get("http://qavalidation.com/demo/")

def test_get_table_data(self):
    time.sleep(10)
    columns = len(self.driver.find_elements_by_xpath(".//*[@id='table01']/tbody/tr[1]/td"))
    rows = len(self.driver.find_elements_by_xpath(".//*[@id='table01']/tbody/tr"))
    print("rows - ",rows)   # rows -  3
    print("columns - ",columns) #columns -  4

    for row in range(rows):
        for col in range(columns):
            values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+row+"]/td["+col+"]").text
            print(" Dynamic web table index {row} ,{col} value is {values} ".format(row, col, values))

@classmethod
def tearDownClass(cls):
    # close the browser window
    cls.driver.quit()

Github sample code https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/DynamicWebTable1.pydynamic we table from http://qavalidation.com/demo/

dynamic we table from http://qavalidation.com/demo/

1
  • Try values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr[%s]/td[%s]" % (row, col)).text Commented May 23, 2018 at 12:48

3 Answers 3

1

This error message...

TypeError: must be str, not int

...implies that while at the mentioned line your program expects a String type of argument where as an Interger type of argumnent was passed to it.

To retrieve the data from the dynamic web table you need to change the line of code as follows :

for row in range(rows):
    for col in range(columns):
        values = self.driver.find_element_by_xpath('.//*[@id="table01"]/tbody/tr["'+row+'"]/td["'+col+'"]').text
Sign up to request clarification or add additional context in comments.

1 Comment

Please, update your answer, because it results in Syntax error. The correct syntax is +"'col'"+ . First double quote ", after ', after col, after ', after double quote ".
0

You can't add string and integer :

>>> "1"+2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    "1"+2
TypeError: must be str, not int

You have to convert int to str :

>>> "1"+str(2)
'12'

Comments

0

It is expecting string value but you are passing int value. It happens when we concatenate string to integer. Please change the following line in your code.

values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+row+"]/td["+col+"]").text

to

values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+str(row)+"]/td["+str(col)+"]").text

It may resolve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.