Trying to accomplish: I'm trying to have the messagebox display the label element. But I'm getting the error posted. The reason for getting the label is trying to make sure the checkmark is checked correctly b/c the checkboxes results are always scattered. I'm trying to find the correct label then put a checkmark on the correct one.
Full XPath:
//html/body/form/table/tbody/tr/td[2]/div[1]/table/tbody/tr[5]/td/table/tbody/tr[1]/td[1]/label]
XPath:
//*[@id="UserAdminForm1_chkListAccess"]/tbody/tr[1]/td[1]/label
Tried:
IWebElement up_names = null;
up_names = caller.driver.FindElement(By.XPath("//*[@id='UserAdminForm1_chkListAccess']/tbody/tr[1]/td[1]/label"));
MessageBox.Show(Convert.ToString(up_names), "Name", MessageBoxButtons.OK);
Error:
[OpenQA.Selenium.NoSuchElementException
HResult=0x80131500
Message=no such element: Unable to locate element: {"method":"xpath","selector":"//*\[@id='UserAdminForm1_chkListAccess'\]/tbody/tr\[1\]/td\[1\]/label"}
(Session info: chrome=96.0.4664.45)
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
at OpenQA.Selenium.By.<>c__DisplayClass19_0.<XPath>b__0(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at Paya.Form1.button1_Click(Object sender, EventArgs e) in \\KS\Project\Form1.cs:line 109
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)][1]
CodePudding user response:
You are getting
no such element: Unable to locate element: {"method":"xpath","selector":"//*\[@id='UserAdminForm1_chkListAccess'\]/tbody/tr\[1\]/td\[1\]/label"}
exception, that could mean following :
- This label is under an iframe.
- This label is under a shadow-root.
- You should not be on the new tab/windows launched by selenium.
- Element was not rendered completely and you were trying to interact with it.
So, first you should check
//input[@id='UserAdminForm1_chkListAccess_0']//following-sibling::label[@for='UserAdminForm1_chkListAccess_0']
XPath is unique or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
if it is unique
and the above conditions
are false, then you could try the below code ;
var up_names = caller.driver.FindElement(By.XPath("//input[@id='UserAdminForm1_chkListAccess_0']//following-sibling::label[@for='UserAdminForm1_chkListAccess_0']"));
And If you selenium version supports Explicit waits then
var up_names = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='UserAdminForm1_chkListAccess_0']//following-sibling::label[@for='UserAdminForm1_chkListAccess_0']")));