How to use Variables in C# code to FindElementbyAttribute?

KitKat21

Client
Регистрация
02.05.2017
Сообщения
263
Благодарностей
191
Баллы
43
Hi guys!

I hope someone can help me.
I'm experimenting with using variables in C# code (thanks to those kind users here who helped me use variables in the code!).

I want to have random settings to emulate moving a mouse/scrolling randomly over different parts of a page to look natural.
So I'm trying to make a C# code with several variables.
1. a variable find element attribute.
2. a variable match #

For example, here's the code (which works) without variables:
HtmlElement he = instance.ActiveTab.GetDocumentByAddress("0").FindElementByAttribute("img", "class", "lazy-image\\ loaded", "regexp", 0);
if (he.IsVoid) return -1;

code with variable (doesn't work):
HtmlElement he = instance.ActiveTab.GetDocumentByAddress("0").FindElementByAttribute(int.Parse(project.Variables["ToMouseTo"].Value));
if (he.IsVoid) return -1;

code with variable (attempt 2 - also doesn't work):
HtmlElement he = instance.ActiveTab.GetDocumentByAddress("0").FindElementByAttribute(int.Parse(project.Variables["ToMouseTo"].Value),int.Parse(project.Variables["RNDmatch"].Value));
if (he.IsVoid) return -1;

this code works with 1 variable:
HtmlElement he = instance.ActiveTab.GetDocumentByAddress("0").FindElementByAttribute("img", "class", "lazy-image\\ loaded", "regexp", int.Parse(project.Variables["RNDmatch"].Value));
if (he.IsVoid) return -1;



The problem I'm having:
C# code works when I just add a variable for the match # but not when I add a variable for the find element attribute.
I even tried to put the two variables together into one and then use that in the code but it keeps failing. :(
I don't know C# so I'm probably doing something wrong here. It's driving me nuts.

I keep getting this error message:
"Compile code of Error in action "CS1501" "No overload for method 'FindElementByAttribute' takes 1 arguments". [Row: 2; Column: 18]"

What does that mean?

Any help is much appreciated!! Thanks!
 

ssXXXss

Client
Регистрация
23.12.2014
Сообщения
7 379
Благодарностей
2 041
Баллы
113
Nапишу по руски, у тебя как я понимаю уже объявлена переменая he, чтобы сработала она дальше или he или HtmlElement he1
 

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
Since the expressions says (string, string, string, string, int), you must convert these values from int to string.
Try:
Convert.ToString(int.Parse(project.Variables["ToMouseTo"].Value)),Convert.ToString(int.Parse(project.Variables["RNDmatch"].Value))
 
  • Спасибо
Реакции: KitKat21

KitKat21

Client
Регистрация
02.05.2017
Сообщения
263
Благодарностей
191
Баллы
43
Since the expressions says (string, string, string, string, int), you must convert these values from int to string.
Try:
Convert.ToString(int.Parse(project.Variables["ToMouseTo"].Value)),Convert.ToString(int.Parse(project.Variables["RNDmatch"].Value))

Thanks for explanation and code! That sounds logical.

It still tells me these errors when I try to do it with the Convert.ToString:

Compile code of Error in action "CS1026" ") expected". [Row: 2; Column: 215]

And this error:
Compile code of Error in action "CS1501" "No overload for method 'FindElementByAttribute' takes 2 arguments". [Row: 2; Column: 18]
 

enrc66

Client
Регистрация
12.10.2013
Сообщения
89
Благодарностей
8
Баллы
8

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
Just check hint, it says how many attributes you must pass over the function.
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)