简体   繁体   English

QTP行数和列数始终为1

[英]QTP row and column count is always 1

I am writing a piece of code to copy data from a web table to an excel sheet. 我正在编写一段代码,以将数据从Web表复制到Excel工作表。 I don't understand why I am getting row count and column count 1. Is there something else that I need to add? 我不明白为什么要得到行数和列数1.我还需要添加其他内容吗?

Here is my code: 这是我的代码:

Dim XL
Set XL=createobject("Excel.Application")
XL.Workbooks.Open "D:\QTP\RailwaysforSurat.xlsx"
Set nsheet = XL.Sheets.Item(1)
row=Browser("title:=.*").Page("title:=.*").WebTable("html tag:=TABLE", "index:=0").GetROProperty("rows")
msgbox row
cols=Browser("title:=.*").Page("title:=.*").WebTable("html tag:=TABLE", "index:=1").GetROProperty("cols")
msgbox cols

This not complete code. 这不是完整的代码。 I have trouble getting rows and columns count. 我很难获取行数和列数。 Please help! 请帮忙!

PS I am using this website for the testing " http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi " 附言:我正在使用该网站进行测试“ http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi

The indianrail page did not open so cannot see the table you are testing. indianrail页面未打开,因此看不到您正在测试的表。 But QTP provides default methods for rows and columns, try using that and see if it works. 但是QTP提供了行和列的默认方法,请尝试使用它,看看是否可行。

NumRows = Browser("Mercury Tours").Page("Search      Results").WebTable("OutboundFlights").RowCount

NumColumns = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ColumnCount(1)

ColumnCount takes row number as parameter

I noticed that you are using Descriptive Programming techniques to identify the WebTable, but you're using extremely generic descriptions for everything. 我注意到您正在使用描述性编程技术来标识WebTable,但是您对所有内容都使用了非常通用的描述。 Perhaps that's just scrubbed for the sake of posting publicly, but the important thing that I notice is that your code to read Rows is trying to find a table with Index of 0 and your code to read Cols is trying to find a table with index of 1... 也许只是为了公开发布而进行了清理,但是我注意到的重要一点是,您读取Rows的代码正在尝试查找索引为0的表,而读取Cols的代码正在尝试查找索引为0的表1 ...

This means you are accessing two different tables. 这意味着您正在访问两个不同的表。 Are you sure that your descriptions are finding the table that you think you want? 您确定您的描述正在找到您认为想要的表吗?

My suggestion is to: 我的建议是:

1) bring up the page in your browser so that you can see the table. 1)在浏览器中调出页面,以便您查看表格。 2) Bring up QTP and open the GuiSPY 3) Click the pointing hand over a cube button to begin spying 4) Click on something inside the table you are trying to work with. 2)调出QTP并打开GuiSPY。3)单击指向立方体按钮的指针以开始监视。4)单击要使用的表格内的东西。

GuiSPY will snap back and show a hierarchy of objects that it found. GuiSPY将回弹并显示找到的对象的层次结构。 Next, you want to positively identify what level in that hierarchy is the table at... I would... 接下来,您想肯定地确定该表在该层次结构中的哪个级别...我会...

5) starting at the top (Browser), select the top row and click "Highlight in Application" and watch what gets lit up. 5)从顶部(浏览器)开始,选择第一行,然后单击“应用程序中的突出显示”,然后观察点亮的内容。 6) go down the hierarchy list clicking the next item in the hierarchy and clicking "Highlight in Application" until you see it flash on the exact table you're trying to target. 6)在层次结构列表中单击,单击层次结构中的下一项,然后单击“突出显示应用程序”,直到看到它闪烁在您要定位的确切表上。 7) Once you have isolated the table, click the button for "Copy the identification properties to the clipboard" button, then close GuiSPY. 7)隔离表格后,单击“将标识属性复制到剪贴板”按钮,然后关闭GuiSPY。 8) Open a notepad, or just use QTP's editor window itself and paste in what GuiSPY copied to clipboard. 8)打开一个记事本,或者只使用QTP的编辑器窗口,然后粘贴复制到剪贴板的GuiSPY。

Ok, you now have a complete list of everything QTP was able to see about the specific table you want to detect. 好的,您现在有了QTP能够查看的有关您要检测的特定表的所有内容的完整列表。 From here, you want to look through the list of properties and find one (or two) that would positively identify that table every time.. For example, on this very page, the table that holds your question could be identified as: WebTable("text:=I am writing a piece of code to copy data")... (*note I've shaved it down because it's automatically a Regex string... With some cleanup, it could be: 从这里开始,您想要浏览属性列表,并找到一个(或两个)每次都会肯定标识该表的属性。例如,在此页面上,可以将保存您的问题的表标识为:WebTable( “ text:=我正在写一段代码来复制数据”)...(*请注意,我已经将其剃光了,因为它自动是Regex字符串...经过一些清理,可能是:

Browser("StackOverFlow.Com").Page("Question 36663629").WebTable("text:=I am writing a piece of code to copy data"))

Now replace your .WebTable("html tag:=TABLE", "index:=0") with that data you've selected, and try it again. 现在,用您选择的数据替换.WebTable(“ html tag:= TABLE”,“ index:= 0”),然后重试。 Hopefully you can lock in on the exact table you're expecting and get the info you need. 希望您可以锁定期望的确切表并获取所需的信息。

Not sure what tables you are looking at, but if it is Services and information then the rows/cols value for those table is 1.The whole web page is consisted of concatenated web tables. 不确定要查看的表是什么,但是如果它是Services and information,则这些表的行/列值为1。整个网页由串联的Web表格组成。 So its going to be little tricky to capture that information. 因此,捕获这些信息并不容易。 Try using a child object method and count the number of links within the web table, its going to look something like this - 尝试使用子对象方法并计算Web表中的链接数,它看起来像这样-

Set oWebEdit=Description.Create
oWebLink("micclass").value="Link"
Set olink = Browser("title:=.*").Page("title:=.*").WebTable("").childobjects(oWebLink)
 olink.count
 msgbox olink.count 

'Then initiate a for loop '然后启动一个for循环

For i = 0 to olink.count-1

' Get the link name '获取链接名称

olink. count(i).GetRoproperty("name")

'Initiate an array and save the link names '启动一个数组并保存链接名称

If get link name does not work then you can use childitem method. 如果获取链接名称不起作用,则可以使用childitem方法。 Also make sure you are using the correct index for the tables or define some other properties as well. 还要确保您为表使用了正确的索引,或者还定义了一些其他属性。

PS If you are going to use DP in future and haven't already then read about Childitems/childobjects methods.They come in real handy while using DP. PS:如果您将来将要使用DP,但尚未阅读有关Childitems / childobjects方法的信息,那么在使用DP时,它们将非常有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM