Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver C# - get text

I have this Html element on the page:

<li id="city" class="anketa_list-item">
   <div class="anketa_item-city">From</div>
    London
</li>

I found this element:

driver.FindElement(By.Id("city"))

If I try: driver.FindElement(By.Id("city")).Text, => my result: "From\r\nLondon".

How can I get only London by WebDriver?

like image 873
Sergey Avatar asked Jul 11 '14 06:07

Sergey


People also ask

Does Selenium support C language?

> Selenium WebDriver supports various programming languages like Java, Python, C#, Ruby, Perl, PHP, JavaScript, R, Objective-C and Haskell.

Can I use Selenium with C++?

Webdriver++ is a C++ client library for Selenium Webdriver which you have to install and have the following feature support: Chainable commands. Value-like objects compatible with STL containers.

Which is better Selenium with Java or C#?

When you start with Selenium I would say JAVA would be a better option for many reasons: There are a lot of opportunities for Selenium with Java is higher in the market as there are very fewer opportunities in C# with Selenium.

What is a Selenium WebDriver?

Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Selenium WebDriver allows you to choose a programming language to create test scripts.


2 Answers

You could easily get by using class-name:

driver.FindElement(By.Class("anketa_item-city")).Text;

or using Xpath

driver.FindElement(By.Xpath("\li\div")).Text;
like image 184
Madhu Avatar answered Oct 27 '22 13:10

Madhu


You can try this:

var fromCityTxt = driver.FindElement(By.Id("city")).Text;
var city = Regex.Split(fromCityTxt, "\r\n")[1];
like image 26
Bart Wojtala Avatar answered Oct 27 '22 12:10

Bart Wojtala