How to check the language of any text present on Website using Selenium
<p>I am testing a website which contains user reviews for hotels which can be in any lang...i.e, Czech or russian or spanish or english etc... and have a question about how can i check in which language the text is by reading the text using Selenium. </p>

<p>for example i am using to element.text method of selenium to read the text, then i want to place a check if its in english then do this else for any other language do that.</p>

<p>This is one of the html element</p>

<pre><code>&lt;div class="innerBubble"&gt;
&lt;div class="quote"&gt;&lt;a href="/ShowUserReviews-g1-d8729164-r427772133-TAP_Portugal-World.html#CHECK_RATES_CONT" onclick="ta.setEvtCookie('Reviews','title','',0,this.href); setPID();" id="r427772133"&gt;â&lt;span class="noQuotes"&gt;TRES SATISFAITS&lt;/span&gt;â&lt;/a&gt;&lt;/div&gt;
&lt;div class="rating reviewItemInline"&gt;
&lt;span class="rate sprite-rating_s rating_s"&gt; &lt;img class="sprite-rating_s_fill rating_s_fill s40" width="56" src="https://static.tacdn.com/img2/x.gif" alt="4 of 5 stars"&gt;
&lt;/span&gt;
&lt;span class="ratingDate relativeDate" title="October 13, 2016"&gt;Reviewed 3 days ago
&lt;span class="new redesigned"&gt;NEW&lt;/span&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;div class="googleTranslation reviewItem"&gt;
&lt;span class="link" onclick="ta.call('ta.overlays.Factory.reviewTranslate', event, this, '/MachineTranslation?g=1&amp;amp;d=8729164&amp;amp;r=427772133&amp;amp;page=review&amp;amp;sl=fr&amp;amp;tl=en'); ta.trackEventOnPage('Reviews', 'google_translate')"&gt;
&lt;img alt="Google Translation" src="https://static.tacdn.com/img2/buttons/googleTranslation.gif"&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div class="entry"&gt;
&lt;p&gt;
Un peu d'apprÃ©hension avant mais vite levÃ©e. TrÃ¨s bon accueil et bon service de la part des pnc, repas chaud et bon, mÃªme pour ce court vol (1h50). Bonne ponctualitÃ© et embarquement des plus efficaces
&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>Detect a language it is not trivial unless the html tag put the current lang.</p>

<p>If you are using selenium in python you can use this function, for that you need to install nltk and the corpus stopwords:</p>

<pre><code>from nltk import word_tokenize
from nltk.corpus import stopwords
def detect_lang(text):
    lang_ratios = {}

    tokens = word_tokenize(text)
    words = [word.lower() for word in tokens]

    for language in stopwords.fileids():
        stopwords_set = set(stopwords.words(language))
        words_set = set(words)
        common_elements = words_set.intersection(stopwords_set)

        lang_ratios[language] = len(common_elements)
    return max(lang_ratios, key=lang_ratios.get)
</code></pre>

<p>With this function you can ask for the lang used:</p>

<pre><code>lang = detect_lang(text)
    if(lang == 'english'):
         ...
</code></pre>

python selenium-webdriver