I want to load a web page in the Selenium Chrome driver and search a few texts on the loaded HTML page. I want to programmatically check if the search text is found or not. I found a way to search in the source code but couldn't find a way to search on the HTML page. XPath search would not work as I want to use the solution with different HTML pages. Could someone suggest how this can be done? I am using the Csharp language.
I think you can do this with Xpath. The following will return an array of any elements on the page with text "text you want to search"
driver.FindElements((By.XPATH, '//*[text() = "text you want to search"]'));
Or, the following will grab any elements containing "my text":
driver.FindElements((By.XPATH, '//*[text()[contains(.,'my text')]]'));
You can then find the length of that array, or interact with the elements specifically. As long as you are just searching for that text, I think this should work on any web page.
Related
enter image description here
I need to click the 'Practice Form' tag highlighted in the attached image using C# Selenium here.
The url of form is https://demoqa.com/forms
and the upon clicking 'Practice Form' it will be redirected to https://demoqa.com/automation-practice-form.
I need to achieve this through clicking, not by navigating.
This XPATH should perfectly work:
//span[contains(.,'Practice Form')]//ancestor::li[#class='btn btn-light ']
Short explanation:
You find span containing specified text
You go to ancestor to get button's xpath.
You need to find it with selenium and use Selenium's click()
This is one of xpaths you can use.
Another option is just going a level up:
//span[contains(.,'Practice Form')]/..
I like both.
Option 3:
//span[contains(.,'Practice Form')]//parent::li[contains(#class,'btn btn-light')]
I am not sure why yours isn't working, but this works fine for me:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.demoqa.com/forms");
driver.FindElement(By.XPath("//span[contains(.,'Practice Form')]//parent::li[#id='item-0']")).Click();
It takes me to:
https://www.demoqa.com/automation-practice-form
With the Student Registration form shown in the middle
BTW in your original comments you said you had to use XPath for this because that field is "dynamic". It is not dynamic. The element has more than one ID: item-0, maybe that is what you meant by couldn't use ID. But the element itself is not "dynamic"
This simple XPath works for me
//span[text()='Practice Form']
You may use the below xpath
//li[#class[contains(.,'btn-light')]][contains(.,'Practice Form')]
I am a test automation engineer who is Working in selenium for the last few years This is my first post in Stackoverflow.
I am trying to fetch the text from an element, but the selenium is not even identifying the element. I am using the specflow setup in visual studio and c# as the language.
Below is the sample html code (This is just a sample structure of the HTML code, not the original one) :
<p class="day">
<meta atts="somevalue">
<span>Some text</span>
<meta atts="somevalue">
<meta atts="somevalue">
"Active"
</p>
I want to fetch the text Active from the above HTML.
I wrote multiple xpaths for this, when i check this in chrome the xpaths are correct and it is highlighting the element
Xpaths trials :
1. //p[#class='day']/text()
2. //p[#class=''day]/meta[#atts='somevalue'][3]/following-sibling::text()
3. few more..
for all the xpaths text() comes in between or at the end. all these are correct and unique when i manually checked it.
there are no frames involved in the web page i am automating. there is no page loading issue also. i have tried with with different wait statements. also tried giving a long thread.sleep().
the location of the element is always same. only the value in it will get changed. ie, the xpath for this element is not dynamic.
I am getting the error in below line. (driver is initialized properly, it is working for everything else)
String StatusVal = driver.FindElement(By.XPath("(//meta[#itemprop='closes'])[1]/following-sibling::text()")).Text;
whatever i try i am getting the below error saying element is not found,
OpenQA.Selenium.NoSuchElementException: Could not find element by: By.XPath: (//meta[#itemprop='closes'])[1]/following-sibling::text()
The above is actual xpath, if i manually check the xpath once the test is failed in the same browser it is available there and it is unique.
ie, in my analysis my xpath is correct, there are no frames involved , there is no wait issue in the page as i am performing actions to the near by elements successfully and i am giving more than enough time for the page to load. all the normal selenium setups are done in my system and everything is working fine other than this one.
Selenium version: 3.12.0
As per the HTML you have provided to retrieve the text Active you can use ExecuteScript() method from IJavaScriptExecutor Interface as follows :
IWebElement myElem = driver.FindElement(By.XPath("//p[#class='day']"));
string myText = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].lastChild.textContent;", myElem);
Console.WriteLine(myText);
I'm making an application that can find an item on a website if the item name and colour matches the one set inside the application.
If the item name is set as "backpack" and colour set as "green" the application should find a match on the page and click the link. The website is this: Click
I would prefer doing this in C# with http requests or something similar. I would also do PhantomJS if anyone has a better solution using it.
You can use selenium, it basically allows you to act like a user with actual web browser. http://www.seleniumhq.org/
You can do something like mentioned below with the help of XPath:
driver.Navigate().GoToUrl("http://www.supremenewyork.com/shop/all/bags");
var backpack= driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//h1//a[contains(., 'Backpack') or contains(., 'Backpack')]"));
var colorGreen = driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//p//a[contains(., 'Acid Green') or contains(., 'Acid Green')]"));
if (backpack.Text == "Backpack" && colorGreen.Text == "Acid Green")
colorGreen.Click();
It is a tested code, it successfully finds the required values within tags, clicks and moves to that page.
Hope it helps.
I am using TFS API and I try am trying to access a property "Description" of an object "WorkItem"
I want to display this property on a web page.
When I display this on the web page , this is what I see :
<p>This task is created for our SSRS team Sesame project.</p>
Firstly , I wanted to know if this is a html tag or does it mean something else in TFS .
And secondly , is there a way I can display this in plain text ?
This task is created for our SSRS team Sesame project.
Please let me know.
If you just want to remove tags, see this simple solution (it uses Regular Expressions):
Remove HTML tags in String
It could be even better if you create it as a extension method.
This is HTML as the System.Description field is an HTML one.
If you just write the field out to a tag it should render as text without the tags. The browser hosting the webpage will render them like any other HTML tags.
Basically i have a webpage with embedded css and JavaScript, so what i want to do is extract only the HTML itself, from texts to tables , images and what not.
So far i have the whole web page stored into a string called "html" the contents of this page is just the facebook hompepage for example,but as you will see there's all scripts and other embedded stuff which i don't want to have.
HTMLEdit = //webpage I chose to store in here//
string html = HTMLEdit.DocumentText;
String result = "this i want to only contain the <head>,<body>,<foot>."
I am only interested in displaying the result witch only contains html, i don't want the JavaScript or css or any other stuff
I have looked at the agility pack but there's no documentation on there website to do this and this is my first ever c# project i have decided to make, so excuse my ignorance if i don't make sense.
See this question
HTML Agility Pack strip tags NOT IN whitelist
Maybe adapt that answer, and drop link and script tags.