0

I am working with the Selenium API in C#. I try to read a script and get the needed values from that.

string attackable_villages = web.FindElement(By.XPath("//*[@id='content_value']/script[1]")).GetAttribute("innerHTML");
richTextBox1.Text = attackable_villages;

The result I get:

result.scrollBound = {
    x_min: 0,
    x_max: 999,
    y_min: 0,
    y_max: 999
};

result.tileSize = [53, 38];

result.screenKey = 'bf14559d';
result.topoKey = 4133874391;

and many more..

But there is this:

result.thisiswhatiwant= [1value1, 1value2, 1value3, 1value4..], [2value1, 2value2, 2value3, ...], [...]

How can I just get that array without the rest and split all elements after "],"

I hope you can help me!

1
  • Why is this tagged with PHP? Commented May 23, 2017 at 13:50

1 Answer 1

1
// Split the script in its individual statements
var splitArray = script.Split(';');
// Get the statement you're interested in
var line = splitArray.Where(x => x.Contains("result.thisiswhatiwant")).First();         
// Remove all characters except numbers and commas, then split by comma       
var values = Regex.Replace(line, "[^0-9,]", string.Empty).Split(',');

Testing with this input:

 string script = "test; result.thisiswhatiwant=[1,2,3]; three;";

gives me an array of length 3 (with values 1/2/3 respectively). You can also use Single(...) instead of Where(...).First() if the statement occurs only once.

Sign up to request clarification or add additional context in comments.

2 Comments

and I can use the split method again in my "line" to split the categories with like .Split(']') ?
Yes, try to experiment with the possibilities, you should get there. You can always use a Regex.Match if you need more flexibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.