Adrija Adrija - 4 months ago 13
C# Question

Clicking on a field name whose unselectable is on using Selenium

I am trying to click on a field which is exactly not clickable. I am attaching the screenshot of the screen.

The Html code behind the page is:

<td class="x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME " style="width: 234px;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-TRAVNAME" unselectable="on">ARUNACHALAM/SHAN</div>
</td>


The code that I have written is in C# which is as follows:

Thread.Sleep(1000);
Driver.Instance.FindElement(By.XPath("//*[@id='ext - gen13']/div/table/tbody/tr/td[3]/div")).Click();


Its throwing exception saying it is unable to find the field.

Can someone please help?enter image description here

Answer

Try using WebDriverWait as below :-

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20));
var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td.grid3-td-TRAVNAME div.x-grid3-col-TRAVNAME")));
el.Click();

Edited : If unfortunately it's not clickable due to unselectable="on", remove this attribute property before clicking using IJavascriptExecutor as below :-

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20));
var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td.grid3-td-TRAVNAME div.x-grid3-col-TRAVNAME")));

IJavaScriptExecutor js = Driver.Instance as IJavaScriptExecutor;
el = (IWebElement)js.ExecuteScript("arguments[0].removeAttribute('unselectable'); return arguments[0];", el);
el.Click();

Edited :- cssSelector does not work here try using By.Xpath() as below :-

var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.Xpath(".//div[contains(text(), 'ARUNACHALAM/SHAN')]")));