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
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();
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();
I am trying to find iframe within iframe using Selenium WebDriver. I have a scenario where a popup opens in a website which itself is an iframe and this in turn contains 4 richedit textboxes which are nothing but iframes. Here is how i am trying to do it. If i check the foreach loop, i get 4 iframes correctly which suggests that i am reaching right elements. What can be the reason for failure for not able to get correct tags inside iframe? Here is my code.
ReadOnlyCollection<IWebElement> topItem = driver.FindElements(By.XPath("//iframe[#src='Edit.aspx']"));
Utility.Instance.WaitUntilFrameLoaded(topItem[0]);
Thread.Sleep(2000);
ReadOnlyCollection<IWebElement> elements = Utility.Instance.WaitUnitElementsAppearsByXPath("//div[#id='ctlPanel']//table/tbody/tr[2]//iframe[#src='javascript:true;']");
Thread.Sleep(1000);
foreach(IWebElement eleFrame in elements)
{
Utility.Instance.WaitUntilFrameLoaded(eleFrame);
IWebElement body = driver.FindElement(By.TagName("body"));
body.Click();
driver.SwitchTo().DefaultContent();
Utility.Instance.WaitUntilFrameLoaded(topItem[0]);
}
However i always get the message that the element with tag name body not found. If i debug, i see html which contains a body tag. I am not sure why it does not provide me back with the element.
I just started using web driver using C#. I am having issue to find that image element to click on it. Below is the HTML for that. Would be great if anybody can help me out. Thanks.
<a id="3245248" class="detail-info" href="#">
<img title="Order Information" src="/Content/images/24x24/info.png">
Assuming your webdriver mentioned is the Selenium WebDriver Nuget package, and assuming your anchor tag has a closing tag after your img tag, you should be able to use Selenium to select the img tag like this:
IWebDriver driver; //previously instantiated.
driver.FindElement(By.CssSelector("#3245248 img")).Click();
Alternatively, just click on the anchor tag itself:
IWebDriver driver;
driver.FindElement(By.Id("3245248")).Click();
The trick here is to understand CSS selectors. # precedes an id selector and putting a space and tag name after that is a child selector. So in short, select the anchor tag by id and look inside it for an img tag.
Another helpful tip to understand with Selenium, if you can select it using your web browser's JavaScript console by calling document.querySelector('some css selector') then Selenium will be able to select it as well.
If this does not help, please update your question to be more specific.
You'll want to click the anchor tag, rather than the image itself. You can try this XPath to find the anchor tag if the id is unique and not going to change
"//a[#id='3245248']"
Or a little safer, if the ID is dynamic, find the anchor tag that has your image inside it:
"//a[./img[#title='Order Information']]"
Or this CSS Selector, again, only if the ID is unique and not going to change
"a#3245248"
EDIT: Use this rather than the FindsBy annotation that I think you're using, which is susceptible to break if an element is dynamically added/modified after the page loads
IWebElement link = driver.FindElement(By.XPath("//a[./img[#title='Order Information']]"));
link.Click();
If you debug this, you should see whether or not it actually finds the IWebElement first
css=a.detail-info > img[src='/Content/images/24x24/info.png'] This CSS can be used.
Let me know is this CSS Selector is working or not.
I was not able to get satisfactory answer after searching on google.
So could you please guide me on this?
I have a div containing li,a,labels below it.
I am able to find the div using CssSelector by its class name.
Now inside this div I want to get a label with its text and then click on it.
The label is as belo:
<label>Sign Out</label>
How to do that ?
I have a working solution using XPath and iterating over all labels inside div, but I am unable to get it using CssSelector.
My Solution:
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath("//label[text()='Sign Out']"));
or
by using foreach:
var coll = menu.FindElements(By.TagName("label"));
foreach (var label in coll)
{
if(label.Text.Trim() =="Sign Out")
{
Log("Sign out was found.");
label.Click();
break;
}
}
I tried with CssSelector:
IWebElement logoutLabel = menu.FindElement(By.CssSelector(":contains('Sign Out')"));
IWebElement logoutLabel = menu.FindElement(By.CssSelector("label:contains('Sign Out')"));
IWebElement logoutLabel = menu.FindElement(By.CssSelector("label['Sign Out']"));
But these are not working.
There is a very good reason why your CSS selector wouldn't work, specifically the contains bit is where it falls over. Why?
It isn't part of the CSS selector specification, and therefore would never work.
The contains that we all know and love is actually coming from Sizzle, the CSS selector engine behind jQuery.
If you want text-based searching, you will either have to use XPath, or get a collection of those elements (using any locator you see fit) and then filter them down in code (like you have done in your foreach loop). There isn't a native CSS-style way to do "text based searching".
In terms of your current code, you will probably also fall over because XPath requires a little "poking" to tell it to search only the child elements of your current "element".
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath("//label[text()='Sign Out']"));
Should be:
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath(".//label[text()='Sign Out']"));
(Note the "." in the XPath)
Also, your foreach loop should have worked, therefore you will have to put a breakpoint in there and check exactly what is being returned by menu.FindElements(By.TagName("label"));.
You are using :contains(), which is a jQuery selector, not a CSS selector.
Apparently there is no way to achieve such thing using only CSS.
More info CSS selector based on element text?