我正在使用 selenium 从 txt 文件中循环遍历 URL,并获取每个 URL 上列出的特定序列号。然后我想引用一个 excel 文件并找到序列号旁边的单元格并打印该值。我的问题是当我将序列号存储在一个变量中时,它在我的 if 语句中不起作用。如果我用实际序列号替换变量,它会按预期运行。为什么存储的变量在我的代码中不起作用?这是我的预期代码,我没有收到任何输出: with open('Url.txt') as links: for line in links: time.sleep(3) driver.get(line) editpage = driver.find_elements_by_link_text('Edit this device') for page in editpage: time.sleep(1) editurl = page.get_attribute('href') driver.get(editurl) Devicesn = driver.find_elements_by_id('device.name') for sn in Devicesn: serialnumber = sn.get_property('value') wb = xlrd.open_workbook('C:\doc.xls') for sheet in wb.sheets(): for row_num in range(sheet.nrows): row_value = sheet.row_values(row_num) if row_value[1] == serialnumber: print(row_value[0])如果我用数字替换序列号,我会得到我正在寻找的单元格值: with open('Url.txt') as links: for line in links: time.sleep(3) driver.get(line) editpage = driver.find_elements_by_link_text('Edit this device') for page in editpage: time.sleep(1) editurl = page.get_attribute('href') driver.get(editurl) Devicesn = driver.find_elements_by_id('device.name') for sn in Devicesn: serialnumber = sn.get_property('value') wb = xlrd.open_workbook('C:\doc.xls') for sheet in wb.sheets(): for row_num in range(sheet.nrows): row_value = sheet.row_values(row_num) if row_value[1] == 6216295237: #replaced serialnumber print(row_value[0])这给了我预期的价值。为什么我的序列号变量在这里不起作用?谢谢!
2 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
尝试打印您的变量:
print(serialnumber)
和
print(row_value[1])
然后,检查它们是否相同。
编辑:查看文档,您应该尝试使用get_attribute()
而不是get_property
添加回答
举报
0/150
提交
取消