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']"
Related
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();
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();
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?
I know when i want to scroll down to specific element in appium use the following
driver.ScrollTo(value);
but this value is changed every time and can't detect it i can not use this value to scroll until find the element, but this element is the last element in my page and number of element in the page is changed between user and another.
So, there is any other way to scroll down till the end of the page ?
Use xpath to find the element without using value(as it is dynamic)
then use that element to scroll,
WebElement element = driver.findElementByXpath("xpath_of_element");
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Just post the DOM of that page..i'll give you efficient xpath so even if your dom is dynamic that locator will work...
scrollToExact and scrollTo method is depricated since JAVA client version 4.0.0 (https://github.com/appium/java-client/blob/master/README.md).
You can use UiScrollable and UiSelector and their methods to scroll any page (https://developer.android.com/reference/android/support/test/uiautomator/UiScrollable.html).
Example of JAVA code (please edit it with C# syntax):
#AndroidFindBy (uiAutomator = "new UiScrollable(new
UiSelector()).scrollIntoView(new UiSelector().textContains(\"Question
\"))")
If you want scroll to the bottom of page, please create method that will do several steps to find last element of page, otherwise you will get an error (Element not found)
Use touch action instead
TouchAction t = new TouchAction(driver);
t.longPress(element(source)).moveTo(element(destination)).release().perform();