I want to perform different operations based on nation name with Selenium. However, I think I'm making a mistake in the condition that the codes are not working. How can I meet this condition?
if (nationality.Trim()==("T.C."))
{
var nationality =
drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[1]/div/div/div[2]/h4")).Text;
if (nationality.Trim()==("T.C."))
{
var val4 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[2]/div/div/div[1]")).Text;
Thread.Sleep(3000);
var val5 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[3]/div/div/div[1]")).Text;
Thread.Sleep(3000);
var val6 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[9]/div/div/div/input")).GetAttribute("value");
Thread.Sleep(3000);
var val7 = drv.FindElement(By.XPath("/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[2]/div/div[2]/div/div/h4")).Text;
}
else
{
var val4 = drv.FindElement(By.XPath($"/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[2]/div/div/div[1]")).Text;
Thread.Sleep(3000);
var val5 = drv.FindElement(By.XPath($"/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[3]/div/div/div[1]")).Text;
Thread.Sleep(3000);
var val6 = drv.FindElement(By.XPath($"/html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[4]/div/div[2]/div/div/div/div[9]/div/div/div/input")).GetAttribute("value");
var val7 = drv.FindElement(By.XPath($"//html/body/form[2]/div[4]/div/div[3]/div/div/div[1]/div/div[2]/div/div[2]/div/div/input")).GetAttribute("value");
Naimish MakwanaPosted Jan 8, 2024, 4:02 AM
It seems like you’re trying to perform operations based on the nationality, but the nationality variable is being used before it’s defined. You should move the line where you define the nationality variable to the top, before the
ifcondition. Here’s how you can modify your code:This should fix the issue you’re facing.
Thanks
Mehmet FatihPosted Jan 8, 2024, 8:53 AM
Thansk it was solved.