Clear a string varible

I’m getting an attribute from an image on a page, but not all pages have the image. In my For Each Row loop, I’m going to a URL, copying H1 and copying src attribute of an image (if it’s there) and saving it to a variable (strImage). In my output it’s showing the same sting for every URL, which is incorrect. I want to clear the variable when the robot goes to the next row. I added an ‘Assign’ activity before navigating to the next URL and have it set to strImage = “”, but unfortunately, it is not working. What am I missing?

It is generally not a good idea to re-use the same variable for multiple uses. If each string is a different object, you should create a variable for each string you need. If it’s not feasible to create that many individual strings, or there is an unknown amount of strings, then that is exactly what collections are meant for. You can create a list (or any other collection) to store the strings instead. I’d highly recommend going this route instead of using a variable and changing it’s value each time.

However to answer your question, you can empty a string variable in an assign activity YourString = String.Empty

That shouldn’t even be necessary though, you can just re-assign it to your string variable and it will overwrite whatever value you want. If it is showing the same string every iteration of the loop, I have a feeling there is something wrong in the code and you are referencing the wrong variable, or there is a scope issue with the variable

It’s a variable that is being written everytime the robot goes to a new row. So, in context, the robot opens an Excel file, navigates to a URL that is in column A, gets the header at H1, then gets the src code for an image on the page (if it exists). Problem I’m having is that the strImage variable is finding an image in the second URL in the loop, but not another until the 12th pass. The URLs without an image should have a blank cell in the image column, but instead have the src code from the last image it found 12 passes before. I’m trying to clear the variable after writing to the data table so I’m not re-writing data from previous passes. Does that make a little more sense?

Yes that does make sense. It sounds like it’s an issue of variable scope. Make sure the variable scope is set to the body of the for each. That way each iteration it will essentially be a new variable that is created from scratch.

Of course you can always just have the last thing the loop does is assign YourString = String.Empty as well. Either way works.

There has to be something easy that I’m missing. Here is my workflow:
Create dtData from Excel input file
Create dtOutput to store data
Clear dtOutput
Open Browser activity
Do:
For Each Row in dtData
Navigate to: row.item(“URL”).ToString
Get Text ‘H1’ (Save to strHeader)
Get Attribute ‘IMG’ (Save to strImage)
Add Data Row: {row.item(“URL”).ToString,strHeader,strImage}
Assign strImage = String.Empty
Write Range to Excel dtOutput

Do you see anything wrong in that workflow that I should check to see why it’s not clearing the strImage variable?

Nothing obvious, if you uploaded the .xaml i’d likely be able to help quickly enough.

If you aren’t able to upload the .xaml it’ll be tougher, but you could post some screenshots - i’d like to see the variable pane too so I’d know the scope of the strImage variable.

If you can’t post any screenshots or upload the .xaml you can debug it yourself by adding a few write line activities - add one at the beginning of the for each, end of the for each, right before the get attribute & right after the get attribute. As an alternative, you could run it in debug mode to step through it line by line and check the value it is showing for strImage variable at each step

Main.xaml (11.3 KB)

I’ve never uploaded an xaml file before, so I hope it worked. The input data is coming from an Excel file.

Here is an example site I’m trying to get the heading (H1) and the image path for the image right above the header:
https://www.heartrhythmjournal.com/content/sudden_cardiac_arrest

I couldn’t actually run it since i didn’t have the inputs, but it looks like the variable piece is ok. strImage2 is the correct scope and should be reset each iteration (and you have it set to string.empty at the end too).

I’m guessing it is getting the same result each time because of the selector you are using in the Get Attribute activity. It is looking for <webctrl parentid='13619b69-31fb-4f33-841d-9dd5ee624ccc' tag='IMG' /> every single time, which means it is grabbing the same image source every time

Yes, that is probably the issue. Do you know a better way to grab the image dynamically? As you can’t tell, I’m a novice to UIPath

That’s a little tougher to help with, it just requires a little bit of playing around and trying things out. You’ll have to try and use UiExplorer to look at the properties to build a selector that is dynamic enough to grab each image. That could mean you can use something as simple as an index (idx) that corresponds with the row you’re on, or it could be a little tougher and you find headers that match, then within that matching header you can find the image using the ‘find children’ activity. It’s a little tougher to have