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();
Related
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();
hi. I want to click on this placeholder inside div, how can I do it, I tried Xpath but it doesnt work.
It seems your input field is inside an iframe. You have to switch to iframe first and then try to sendkeys.
IWebElement iframeElement= driver.FindElement(By.Name("top"));
driver.SwitchTo().Frame(iframeElement);
IWebElement element = driver.FindElement(By.Id("username"));
element.SendKeys("text");
Note: As in question there is no detail about iframe iframeElement so I took this name from input element. You can change locator if it is not correct
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();
Firebug gives me this for an element inputfiled I want to fill in:
<label for="form:composite:tabView:ssn">Fødselsnummer</label>
Tried this in my selenium script:
WebElement velger = driver.findElement(By.xpath("//input[#id='form:composite:tabView:ssn']"));
Next I do this:
velger.sendKeys(new String[]{"27017833176"});
And then:
WebElement sokknapp = driver.findElement(By.xpath("//*[#id=\"form:composite:tabView:searchSSN\"]"));
sokknapp.click();
To click the serach button.
However when looking at the browser during replay I can se that the "sendkeys" does not work but the button click does work (the inputfield gets red because I press the button for searching without content in the inputfield).
Is there something wrong with this:
velger.sendKeys(new String[]{"27017833176"});
Unable to locate element might means that you need some time to wait before handle element:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var velger = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("form:composite:tabView:ssn")));
Also I'm not sure about correctness of velger.sendKeys(new String[]{"27017833176"});. Try to send simple string:
velger.sendKeys("27017833176");
The problem (or solution) was that I missed i clear() function before sendkeys.
velger.clear();
velger.sendKeys("27017833176");
I am not able to Click on SubMenu item using selenium webdriver using c#.
I am using IE9 and FireFox 13.
I have tried Action Builder but it does not work.
It gives an error saying element cannot be clicked.
WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
Actions builder = new Actions(webDriver);
builder.MoveToElement(menu).Build().Perform();
Thread.Sleep(5);
//then click when menu option is visible
menuOption.Click();
I have used even javascript :
js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();
Please give some solution to click on hidden elements
You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).
The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.
Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.
After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which #Slanec is talking about in the above post.
By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.
I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -
new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver ) {
return driver.findElement(By.Id("ID2")).isDisplayed();
}
});
I hope that this helps you.