I want to combine the three variables that I defined separately in selenium and turn them into a single name. But I couldn't put it together. Where am I making a mistake?
var okulno = drv.FindElement(By.Id("txtOkulNo")).Text; // ögrenci no
var adi = drv.FindElement(By.Id("txtAdi")).Text; // ögrenci adi
var soyadi = drv.FindElement(By.Id("txtSoyadi")).Text; // ögrenci soyadi
var jpgname = "" + okulno + " " + adi + " " + soyadi + ""; // The line I want to solve
WebElement upload_file = (WebElement)drv.FindElement(By.XPath("//*[@id=\"flResimSec\"]"));
upload_file.SendKeys(Application.StartupPath + "\\resimler\\" + jpgname + ".jpg");
Jayraj ChhayaPosted Jan 9, 2024, 12:07 PM
To combine the three variables and assign them to a single name in Selenium using Dot Net, you can use the concatenation operator (+) to join the variables together. However, in the provided code snippet, there seems to be an issue with the concatenation line.
Here's the corrected code snippet:
In the corrected code, I removed the unnecessary empty string concatenation at the beginning of the
jpgnamevariable assignment. Now, the three variables (okulno,adi, andsoyadi) are concatenated with spaces in between them.Make sure to check that the elements with the specified IDs (
txtOkulNo,txtAdi,txtSoyadi) exist on the page and have the expected values before executing this code.By using the corrected code, the three variables will be combined into a single name (
jpgname), which can then be used in the file upload path.Naimish MakwanaPosted Jan 9, 2024, 4:27 PM
It seems like you’re trying to concatenate the text from three elements to form a filename. Your code looks correct, but there might be an issue with the spacing or the way the string is being concatenated.
Here’s a slightly modified version of your code:
This code does the same thing as yours, but uses
string.Formatto concatenate the strings, which can be more readable and performant. If you’re still having issues, the problem might be elsewhere in your code or with the values ofokulno,adi, andsoyadi. Make sure these variables are getting the correct values from your web elements. If the problem persists, please provide more details about the error message or behavior you’re experiencing.Uttam ChaturvediPosted Jan 9, 2024, 3:53 PM
1.Try removing spaces and add hyphen (-), spaces might be causing issue

2. As best practise try using string interpoltion for cleaner code