简体   繁体   English

如何从动态表中检索数据-Selenium Python

[英]how to retrieve data from dynamic table - selenium python

I'm trying to retrieve data from the dynamic web table in selenium python but errors in a console as ERROR as " 我正在尝试从Selenium python中的动态Web表检索数据,但在控制台中将错误显示为“

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

TypeError: must be str, not int TypeError:必须为str,而不是int

FAILED (errors=1) 失败(错误= 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.py Github示例代码https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/DynamicWebTable1.py 动态表格来自http://qavalidation.com/demo/

dynamic we table from http://qavalidation.com/demo/ 动态表格来自http://qavalidation.com/demo/

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. ......暗示,而在上述行程序预计在那里作为argumnent的基于整数类型传递给它一个参数的字符串类型。

To retrieve the data from the dynamic web table you need to change the line of code as follows : 要从动态Web表中检索数据,您需要按如下所示更改代码行:

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

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 : 您必须将int转换为str:

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

It is expecting string value but you are passing int value. 它期望字符串值,但您正在传递int值。 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. 它可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从动态网站python selenium中检索表 - How to retrieve table from dynamic website python selenium 如何从python硒数据构建动态表? - How to build a dynamic table from python selenium data? 如何使用 selenium python 从动态表中提取数据? - How to extract data from a dynamic table with selenium python? 如何使用 selenium python 从动态网站中检索所有链接 - How to Retrieve all links from a dynamic website with selenium python Selenium Python 从动态表中提取数据,每 5 秒刷新一次 - Selenium Python Pull Data from Dynamic Table that refreshes every 5 seconds 使用 Python 和 Selenium 从动态表中抓取数据 - Scrape data from dynamic table using Python & Selenium 使用 python、BeautifulSoup、Selenium 从表中抓取动态数据 - Scrape dynamic data from a table with python, BeautifulSoup, Selenium 如何使用Flask和JINJA2获取python从动态表中检索用户输入的数据 - How to get python to retrieve user-inputted data from a dynamic table using Flask & JINJA2 如何在Python中使用Selenium从具有隐藏元素的动态折叠表中提取数据 - How to extract data from dynamic collapsing table with hidden elements using Selenium in Python 使用 Selenium Python 获取动态表数据 - Getting Dynamic Table Data With Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM