How to click on the button with Selenium C# - 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();

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();

'element not interactable' exception in C# chromedriver

I am using selenium chromedriver in c# to try and click a print button however I am receiving an exception of "element not interactable", here is the website source of the print button:
<p _ngcontent-c27="" class="print"><span _ngcontent-c27="" class="floatRight">Print</span><img _ngcontent-c27="" class="printImg" src="../../../../../assets/images/Print.svg"></p>
What I've tried:
driver.FindElementById("clippedTab").Click(); // Successfully clicks on the 'Clipped' tab
//None of the below worked:
driver.FindElementByClassName("print").Click();
// and
driver.FindElementByClassName("printImg").Click();
// and
driver.FindElementByClassName("floatRight").Click();
However none of these worked for me.
The website used is bjs.com and the print button and can be found on the Clipped tab.
What am I doing wrong, why is the element not intractable and how could I solve this?
The Xpath you are making is actually causing issue.
All the above Xpaths are not unique and actually point to 2 Xpaths 1 is Print you want to click, and other is not interactable(or hidden) one. Since it is trying to click on hidden one, it is throwing Error.
See image
Simple solution :- use XPath as :: //*#id="clipped"]/div/div[1]/div[3]/div/p/span
The element has to be visible (that's usually what makes it "interactable") before you can Click() it. Are you opening the "Clipped" tab before you Click()?
The desired element is an Angular 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:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("img.printImg"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//img[#class='printImg']/h2"))).Click();

Click an Onclick Event in selenium

I am trying click a menu icon which has the following html code
<i class="icon-left-space icon-chevron-sign-down"> </i>
doc_number -Number will change each time
My code to click the icon menu
FindElement(By.ClassName("ctm-icon-link"));
FindElement(By.XPath("//a[#href='#']#onclick"));
FindElement(By.XPath("//a[#href="#"]#class"));
To locate the element with respect to the onclick event you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
XPath 1:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[#class='ctm-icon-link' and starts-with(#onclick, 'show_menu')]")));
XPath 2:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[#class='ctm-icon-link' and starts-with(#onclick, 'show_menu')]/i")));

cant access element inside divs

Using Selenium C# when I click button 1 it throws NoSuchElementException
I tried this:
driver.FindElement(By.XPath("/html/body/div[2]/div[2]"));
driver.SwitchTo().Frame(0);
new WebDriverWait(driver, TimeSpan.FromSeconds(10))
.Until(SeleniumExtras.WaitHelpers.ExpectedConditions
.ElementExists((By.Id("hello-button"))));
[![HTML is attached][1]][1]
[1]:
Since the button has unique id, you should use it, instead of the XPath:
driver.FindElement(By.Id("hello-button")).Click();
Or, if it is the only button on the page
driver.FindElement(By.XPath(".//button")).Click();

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?
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

Categories