Reputation: 88
I am trying to find the ul id "mainTimeSheet" and li item weekly report by id = timesheetReport but not getting it. I am new to C# selenium.
Below is the code i am trying in C# :
//var drop = driver.FindElement(By.XPath("//ul[@id='mainTimeSheet']"));
var drop = driver.FindElement(By.Id("mainTimeSheet")).
FindElement(By.XPath(".//li[@id='timesheetReport']"));
drop.Click();
HTML Code:
<li class="dropdown">
<a href="javascript:;" data-toggle="collapse" data-
target="#mainTimeSheet">
<i class="fa fa-fw fa-tasks"></i> Time Sheet <span class="fa
arrow"></span>
</a>
<ul id="mainTimeSheet" class="nav nav-second-level collapse">
<li>
<a id="timesheetReport" href="/ShaeetsListsadas.aspx"><i
class="fa fa-fw fa-tasks"></i> Weekly Report</a>
</li>
<li>
<a id="submitTimeSheet" href="/SheetsFormsads.aspx"><i
class="fa fa-fw fa-tasks"></i> Submit Timesheet</a>
</li>
<li>
<a id="timeSheetDetailReport" href="/sheetsReportsasd.aspx"<i
class="fa fa-fw fa-tasks">
</i> Day Wise Report</a>
</li>
</ul>
</li>
Upvotes: 1
Views: 183
Reputation: 29362
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
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.
Xpath to check:
//li//a[@id='timesheetReport']
If it shows 1/1 matching node then
click it like this:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement timeSheet = wait.Until(e => e.FindElement(By.XPath("//ul[@id='mainTimeSheet']")));
timeSheet.Click();
IWebElement weeklyReport= wait.Until(e => e.FindElement(By.XPath("//li//a[@id='timesheetReport']")));
weeklyReport.Click();
Update:
to resolve selenium.common.exceptions.ElementNotInteractableException:
You should debug your code like below:
Make sure the browser is launched in full screen using
driver.Manage().Window.Maximize();
Use ActionChains
:
Actions actionProvider = new Actions(driver);
actionProvider.MoveToElement(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(e => e.FindElement(By.XPath("//ul[@id='mainTimeSheet']")))).Click().Build().Perform();
Use ExecuteScript
:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement timeSheet = wait.Until(e => e.FindElement(By.XPath("//ul[@id='mainTimeSheet']")));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click(); ", timeSheet);
Upvotes: 1