I want to create a conditional loop in Selenum. However, I am getting an error on the condition line regarding checkbox. I tried all of the methods below. It gives errors in all of them. What should the condition be like?
if (drv.FindElement(By.Id("chk1")).GetAttribute("checked") != null)
if (isChecked = drv.FindElement(By.Id("chkS1")).GetAttribute("checked").equals("true"))
if (drv.FindElement(By.Id("chkS2")).GetAttribute("checked").Equals("true"))
if (drv.FindElement(By.Id("chkS2")).GetAttribute("checked")..isSelected)
if (drv.FindElement(By.Id("chkS1")).GetAttribute("checked") .Selected())
if (drv.FindElement(By.Id("chk1")).GetAttribute("checked") != null)
{
var yaz1 = dataGridView1.Rows[i].Cells[3].Value.ToString();
drv.FindElement(By.Id("not1_" + (i))).SendKeys(yaz1);
}
else if (drv.FindElement(By.Id("chk2")).GetAttribute("checked") != null)
{
var yaz2 = dataGridView1.Rows[i].Cells[4].Value.ToString();
drv.FindElement(By.Id("not2__" + (i))).SendKeys(yaz2);
}
Naimish MakwanaPosted Jan 12, 2024, 9:34 AM
In Selenium WebDriver, you can use the
isSelected()method to check if a checkbox is selected. Here’s how you can modify your code:In this code,
drv.FindElement(By.Id("chk1")).Selectedwill returntrueif the checkbox with idchk1is selected, andfalseotherwise12. The same applies for the checkbox with idchk2. If you want to select a checkbox based on its current state, you can use a similar approach12.Please note that the
Selectedproperty is used instead of theisSelected()method because you seem toThanks
Mehmet FatihPosted Jan 12, 2024, 9:58 AM
Thans Naimish.I have solved the problem to similiar yours. Mine is like that.
WebElement chk1 = (WebElement)drv.FindElement(By.Id("chkS1"));
WebElement chk2 = (WebElement)drv.FindElement(By.Id("chkS2"));
WebElement chk3 = (WebElement)drv.FindElement(By.Id("chkS3"));
if (chk1.Selected) //1.yazili
{
var yaz1 = dataGridView1.Rows[i].Cells[3].Value.ToString();
drv.FindElement(By.Id("dgListem_lblY1_" + (i))).SendKeys(yaz1);
}
if (chk2.Selected) //2.yazili
{
var yaz2 = dataGridView1.Rows[i].Cells[4].Value.ToString();
drv.FindElement(By.Id("dgListem_txtY2_" + (i))).SendKeys(yaz2);
}
if (chk3.Selected) //3.yazili
{
var yaz3 = dataGridView1.Rows[i].Cells[5].Value.ToString();
drv.FindElement(By.Id("dgListem_txtY3_" + (i))).SendKeys(yaz3);
}