Find element with selenium by display text - c#

I am trying to hover over an element in a menu bar with selenium, but having difficulty locating the element. The element is displayed below :
<DIV onmouseover="function(blah blah);" class=mainItem>TextToFind</DIV>
There are multiple elements of this type so I need to find this element by TextToFind.
I've tried :
driver.FindElement(By.XPath("TextToFind"))
and
driver.FindElement(By.LinkText("TextToFind"))
which both didn't work. I even tried:
driver.FindElement(By.ClassName("mainItem"))
which also did not work. Can someone tell me what I am doing incorrectly?

You are using incorrect syntax of xpath in By.Xpath and By.LinkText works only on a element with text and By.ClassName looks ok but may be there are more elements with that class name that's why you couldn't get right element, So you should try use below provided xPath with text :-
driver.FindElement(By.XPath("//div[text() = 'TextToFind']"));
Or
driver.FindElement(By.XPath("//div[. = 'TextToFind']"));
Or
driver.FindElement(By.XPath("//*[contains(., 'TextToFind')]"));
Hope it works...:)

Better ignoring the whitespaces around the text with this:
var elm = driver.FindElement(By.XPath("//a[normalize-space() = 'TextToFind']"));
This searches text within an [a] element, you can replace it with any element you are interested in (div, span etc.).

Related

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

How to get text after link in htmlagilitypack

I have next part of html code:
<div class="resum_card">
<p>Experience: 5 years</p>
</div>
And what i try inside the code:
nodeValue = hd.DocumentNode.SelectSingleNode("//div[#class='resum_card']//p//a[#class='no_decore']//following-sibling::a");
But it return me null, can anybody help me?
You can try this XPath to get text node after <a> element :
nodeValue = hd.DocumentNode
.SelectSingleNode("//div[#class='resum_card']/p/a/following-sibling::text()");
Note: simply use single slash (/) instead of double (//) to select element that is direct child of current element. It is better performance wise.

C# selenium webdriver css selector for label with only text Sign Out

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?

How to remove nodes using HTML agility pack and XPath so as to clean the HTML page

I need to extract Text from webpages mostly related to business news.
say the HTML page is as follows..
<html>
<body>
<div>
<p> <span>Desired Content - 1</span></p>
<p> <span>Desired Content - 2</span></p>
<p> <span>Desired Content - 3</span></p>
</div>
</body>
</html>"
I have a sample stored in a string that can take me to Desired Content -1 directly, so i can collect that content. But i need to collect Desired Content -2 and 3.
For that what i tried is from the current location i.e from with in span node of desired Content -1 i used parentof and moved to the external node i.e Para node and got the content but actually i need to get the entire desired content in div. How to do it? You might ask me to go to div directly using parentof.parentof.span. But that would be specific to this example, i need a general idea.
Mostly news articles will have desired content in a division and i will go directly to some nested inner node of that division. I need to come out of those inner nodes only till i encounter a division and then get the innerText.
I am using XPath and HTMLagilitypack.
Xpath i am using is -
variable = doc.DocumentNode.SelectSingleNode("//*[contains(text(),'" + searchData + "')]").ParentNode.ParentNode.InnerText;
Here "searchData" is a variable that is holding a sample of Desired Content -1 used for searching the node having news in the entire body of the webpage.
What i am thinking is clean up the webpages and have only main tags like HTML, BODY, Tables, Division and Paragraphs but no spans and other formating elements. But some other website might use Spans only instead of divs so i am not sure how to implement this requirement.
Basic requirement is to extract the News content from different webpages(almost 250 different websites). So i can not code specific to each webpage..i need a generic method.
Any ideas appreciated. Thank you.
This XPath expression selects the innermost div element with $searchData variable reference value as part of its string value.
//div[contains(.,$searchData)]
[not(.//div[contains(.,$searchData)])]
Found out the answer myself...
Using a while loop till i find a div parent and then getting innertext is working.
`{ //Select the desired node, move up till you find a div and then get the inner text.
node = hd.DocumentNode.SelectSingleNode("//*[contains(text(),'" + searchData + "')]"); //Find the desired Node.
while (node.ParentNode.Name != "div") //Move up till you find a encapsulating Div node.
{
node = node.ParentNode;
Console.WriteLine(node.InnerText);
}
Body = node.InnerText;
}`

How to scroll down in appium

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

Categories