Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Get elements html rather Text Value

Tags:

Via that code i have extracted all desired text out of a html document

private void RunThroughSearch(string url) {     private IWebDriver driver;     driver = new FirefoxDriver();     INavigation nav = driver.Navigate();     nav.GoToUrl(url);      var div = driver.FindElement(By.Id("results"));     var element = driver.FindElements(By.ClassName("sa_wr")); } 

though as i need to refine results of extracted document

Container     HEADER -> Title of a given block     Url -> Link to the relevant block     text -> body of a given block /Container 

as u can see in my code i am able to get the value of the text part as a text value , that was fine, but what if i want to have the value of the container as HTML and not the extracted text ?

<div class="container">     <div class="Header"> Title...</div>     <div class="Url"> www.example.co.il</div>     <div class="ResConent"> bla.. </div> </div> 

so the container is about 10 times in a page i need to extract it's innerHtml .

any ideas ? (using Selenium)

like image 248
LoneXcoder Avatar asked May 31 '13 16:05

LoneXcoder


People also ask

How do I getText of elements in Selenium?

Open Firefox browser with the URL: SoftwareTestingHelp.com. Using text method of selenium web driver, find the web element with text – Write and Earn. Validate if the selected element is displayed on the web page. If it is displayed, print the text as Element found using text.

What is the purpose of getText () and getAttribute ()?

The getText() method simply returns the visible text present between the start and end tags (which is not hidden by CSS). The getAttribute() method on the other hand identifies and fetches the key-value pairs of attributes within the HTML tags.

What is the easiest way to get the value from a text field in Selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.


1 Answers

This seemed to work for me, and is less code:

var element = driver.FindElement(By.ClassName("sa_wr")); var innerHtml = element.GetAttribute("innerHTML"); 
like image 67
Oli Wennell Avatar answered Sep 20 '22 14:09

Oli Wennell