Clicking on a field name whose unselectable is on using Selenium - c#

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?

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')]")));

unSelectable attribute
The unSelectable attribute sets whether the selection process can start in an element's content or not. If the unSelectable attribute of an element is set to on, then the element is selectable only if the selection starts outside the contents of the element.
The difference between the unSelectable attribute and the -moz-user-select and -webkit-user-select style properties is that the -moz-user-select and -webkit-user-select style properties specify whether an element can be selected while the unSelectable attribute only specifies whether the selection process can start in an element's content or not. Another difference is that the unSelectable attribute is not inherited while the -moz-user-select and -webkit-user-select style properties are inherited. It means that the unSelectable attribute must be set on all non-selectable elements regardless of whether the unSelectable attribute is set on the parent element of a non-selectable element or not.
This usecase
The relevant HTML would have been helpful to construct a canonical answer. However if the element is a dynamic element or the website is Kendo UI based, then to click on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using WebDriverWait and CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("td.x-grid3-td-TRAVNAME > div.x-grid3-col-TRAVNAME"))).Click();
Using WebDriverWait and XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td[#class='x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME ']/div[text()='ARUNACHALAM/SHAN']"))).Click();
Using IJavaScriptExecutor and CssSelector:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CssSelector, "td.x-grid3-td-TRAVNAME > div.x-grid3-col-TRAVNAME"))));
Using IJavaScriptExecutor and CssSelector:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#class='x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME ']/div[text()='ARUNACHALAM/SHAN']"))));
Reference
You can find a relevent detailed discussion in:
select kendo dropdown using selenium python

Related

How to click on the radio button with label Yes using Selenium and C#

HTML Snapshot:
Element Snapshot:
I want to write xpath for 'Yes' label (Green color mentioned in UI image). I'm new for automation & Please help me to resolve. I have add my HTML code & UI
The radio-button is basically an <input> element associated with the <label> with text as Yes and to click() on it you can use either of the following Locator Strategies:
XPath:
driver.FindElement(By.XPath("//label[contains(., 'Yes')]//ancestor::input[1]")).Click();
Ideally, you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategy:
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[contains(., 'Yes')]//ancestor::input[1]"))).Click();

Click On Highlighted pencil icon using selenium with c#

I have tried like this:
//*[name()='svg']/*[name()='path' and contains(#d, 'M290.74 93.24l128.02']
but it throws exception.
If we use "//*[name()='svg']/*[name()='path'" like this, then it's giving all elements but it's not my actual requirement.
Here I posting some pictures, Please some one help me on this. I tried many ways but I didn't make this possible.
Try getting the selector by inspecting the element and in the Inspector right click the element and copy the css selector.
The desired element is the only descendent <path> element of it's parent <svg> element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//h5[text()='Brandes Intl Equity']//following::*[name()='svg' and #data-icon='pen'][#role='img']/*[name()='path']"))).Click();
Try to select icon based on sibling header value
"//*[name()='svg' and #data-icon='pen']/*[name()='path']"

Unable to locate an element implemented as <td> using Selenium Web Driver

I wanted to locate the value of BNK1 using Selenium web driver by C#. my local website i found have 2 iframe. i have try to change to get my iframe to change to detail frame for me to get my value of BNK1 in the table.
I had no idea what going on with the problem that i not able locate the iframe by using XPath.
but i having error "no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[#class='tabcontentiframe']"}
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[#class='tabcontentiframe']"));
driver.SwitchTo().Frame(detailFrame);
IWebElement element = driver.FindElement(By.XPath("//table/tbody/tr/td[contains(text(),'BNK1')]"));
String text = element.Text;
First, let's get sure that your page is fully loaded before trying anything, using a heavy-hand approach,
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Second, assuming the element you want to access is inside an iframe, you first need to switch to it. Actually, you mention in comment that you have two nested iframes,
// Let's switch to the parent frame
IWebElement tabcontentiframe = driver.FindElement(By.XPath("//iframe[#class='tabcontentiframe']"));
driver.SwitchTo().Frame(tabcontentiframe);
// Let's switch to the child frame
IWebElement iframeHome = driver.FindElement(By.XPath("//iframe[#id='iframeHome']"));
driver.SwitchTo().Frame(iframeHome);
Now, you can do
IWebElement element = driver.FindElement(By.XPath("//table/tbody/tr/td[contains(text(),'BNK1')]"));
// or By.XPath("//td[contains(text(),'BNK1')]")
// or By.XPath("//td[text()='BNK1']")
When you are done, you may want to switch back to the main frame,
driver.SwitchTo().DefaultContent();
You can find the element by using the relative xpath:
IWebElement element = driver.FindElement(By.XPath("//td[text()='BNK1']"));

How to locate an Angular element in Selenium?

I have the following HTML
<div class="dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action" tabindex="0">
::before
</div>
I have tried locating this as follows in Selenium C#
driver.FindElement(By.XPath("//div[contains(#class, 'dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action']")
but the test is failing saying it could not Find Element. I'm trying to click the '+' element shown in the image below.
Not sure what I am doing wrong?
If the element is added dynamically, you need to wait until the element is in the dom.
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
wait.Until(x=> x.FindElement(By.XPath("//div[contains(#class, 'dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action']"));
Instead of putting x.FindElement in the lamba, you can directly do a click. It basically repeats your action until it gets non-null non-exception response, or it timeouts. Giving time for DOM to update.

How to click on the button with Selenium C#

I need to use selenium to click on button but I'm Facing some problems
I try this code but show me error "Selenium.InvalidSelectorException: 'invalid selector"
IList link = driver.FindElements(By.ClassName("button postfix"));
foreach (IWebElement elem in link)
{
if (elem.GetAttribute("ng-click").Equals("quickSearch.search()"))
elem.Click();
}
html page code
<i class="fi-magnifying-glass"></i>
I try to use id but there's no Id for the button so I don't know how to use it
As the element is an Angular element so to invoke click() on the desired element you have to induce WebDriverWait for the ElementToBeClickable and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a.button.postfix[ng-click^='quickSearch'][analytics-event='InventoryManagementSearchKeyword']"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[#class='button postfix' and starts-with(#ng-click, 'quickSearch')][#analytics-event='InventoryManagementSearchKeyword']"))).Click();
You can use Xpath.
driver.FindElement(By.XPath("//a[#class='button postfix' and #ng-click='quickSearch.search()']")).Click();

Categories