我一直在搜索,但无法找到解决此问题的方法。
我有一个iMacros .js,它通过具有以下结构(URL,Value)的.CSV文件,并且: 1.访问URL。 2.将特定值输入到字段中。
冲洗。重复。
但是,即使TIMEOUT值设置为max,仍然会出现无法加载页面的情况。我可以将ERRORIGNORE设置为YES但是我会错过列表中的几个页面而我不会知道它。
因此,我正在搜索的代码是让我运行我的宏,如果页面超时,刷新它并再次运行宏而不跳过CSV中的一行。
这是我的.js的样子:
for (i = 1; i <= count_of_lines; i++) { //The amount of times the Macro will loop should be defined here.
iimSet("i", i) //Set "i" so it is able to extract the nest row from the .csv file
var Macro;
Insert_Rating = "CODE:" //Need to use code here so that iMacros knows that the code will start
Macro += "TAB T=1" + "\n";
Macro += "CMDLINE !DATASOURCE FILENAME.csv" + "\n";
Macro += "SET !DATASOURCE_LINE {{i}}" + "\n";
Macro += "SET !TIMEOUT_PAGE 120" + "\n";
Macro += "URL GOTO=URL{{!COL1}}" + "\n";
Macro += "WAIT SECONDS=1" + "\n";
Macro += "TAG POS={{!COL2}} TYPE=A ATTR=TXT:" + "\n";
}
iimPlay(Macro);
我也试过
var ret = iimPlay(Macro)
if (ret<0) {
iimPlay("CODE:REFRESH");
} else {
iimPlay(Macro);
}
但这里发生的是代码将检查一次(在if的开头),然后在初始条件为真时尝试运行整个CSV。
如果页面超时,我需要一种方法来检查CSV的每一行。
也许这有一个简单的解决方案,但我的编码技巧非常基础。
提前感谢您的帮助。
答案 0 :(得分:0)
我想你差不多了。你需要做的是添加一些逻辑来获得你想要设置的字段。如果找不到该字段,请重试。
这是我能想到的解决方案:
var count_of_lines = 10;
var max_retries = 3;
var macro = '';
for (i = 1; i <= count_of_lines; i++) {
var retries = max_retries;
while (retries > 0) {
macro = "TAB T=1" + "\n";
macro += "CMDLINE !DATASOURCE FILENAME.csv" + "\n";
macro += "SET !DATASOURCE_LINE {{i}}" + "\n";
macro += "SET !TIMEOUT_PAGE 120" + "\n";
macro += "URL GOTO=URL{{!COL1}}" + "\n";
macro += "WAIT SECONDS=1" + "\n";
// trying to extract the field..
macro += "TAG POS={{!COL2}} TYPE=A ATTR=TXT: EXTRACT=TXT" + "\n";
iimSet('i', i);
iimPlayCode(macro);
if (iimGetLastExtract() != '#EANF#' && iimGetLastExtract().trim().length != 0) {
// found the field, do some logic here..
// don't forget to break..
break;
} else {
// if the field could not be found, retry..
retry--;
}
}
}
希望它有所帮助。
答案 1 :(得分:0)
快速更新:我实际上让它以最终需要的方式工作。 :)
var count_of_lines = parseInt(window.prompt("Enter number of lines in the CSV file.","Please enter a number..."));
var DataSource = "/Users/Username/iMacros/Datasources/" + window.prompt("Please enter the name of the file.","");
var Load_Csv = "";
var Timeouts = 0;
for (i = 1; i <= parseInt(count_of_lines); i++) {
iimDisplay("The script has run through " + i + " lines out of a total of " + count_of_lines + "."); //Provide feedback about progress.
iimDiplay("The script has timed out a total of " + Timeouts + " times." ); //Provide feedback about total timeouts.
iimSet("i", i); //Necessary to make the attribution of i here.
iimSet("DataSource", DataSource); //Necessary to make the attribution of DataSource here.
Load_Csv = "TAB T=1 \n";
Load_Csv += "CMDLINE !DATASOURCE {{DataSource}} \n";
Load_Csv += "SET !DATASOURCE_LINE {{i}} \n";
Load_Csv += "SET !TIMEOUT_PAGE 120 \n";
Load_Csv += "URL GOTO={{!COL1}} \n";
iimPlayCode(Load_Csv);
iimPlay("CODE:TAG POS=2 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT"); //We are checking to see if the attribute exists (its length is >0)
if (iimGetLastExtract() != '#EANF#' && iimGetLastExtract().trim().length != 0) {
// Condition is met (Field exists = Page loaded correctly)
iimSet("i", i); //Necessary to make the attribution of i here.
iimSet("DataSource", DataSource); //Necessary to make the attribution of DataSource here.
Insert_Rating = "CMDLINE !DATASOURCE {{DataSource}} \n";
Insert_Rating += "SET !DATASOURCE_LINE {{i}} \n";
Insert_Rating += "TAG POS={{!COL2}} TYPE=A ATTR=TXT: \n";
iimPlayCode(Insert_Rating); //Run the macro
}
else
{
// if the field could not be found, retry..
iimPlay("CODE:REFRESH");
i--; //If the load fails, you should subtract from i in order to make sure that you're not skipping a row in the .csv
Timeouts++; //Adds to the timeouts counter.
}
}