I want to translate a string using Google Translator with HtmlAgilityPack; .
I want to use HTML Agility Pack to parse HTML documents.
I tried this:

using HtmlAgilityPack; 
.............

var webGet = new HtmlWeb();
var document = webGet.Load(
    "http://translate.google.com/#en/bn/this%20is%20my%20string");

var node = document.DocumentNode.SelectNodes(
    "//span[@class='short_text' and @id='result_box']");

if (node != null)
{
    foreach (var xx in node)
    {
        x = xx.InnerText;
        MessageBox.Show(x);
    }
}

But I get no results.

My aim is to translate a complete string using Google Translate and to show the translated string in a label in Windows Forms.

How can I do this?

Dani AI

Generated

Short, practical explanation: the Translate web page you loaded is a client-side single‑page app — the part after the # (for example #en/bn/...) is a fragment processed by the browser (not sent to the server), and the translation is filled in by JavaScript after the page loads. HtmlAgilityPack only downloads and parses the server HTML; it does not execute the page’s JavaScript, so the translated text never appears in the static DOM you parse. This is why ’s request returned nothing. (developer.mozilla.org)

Short-term recommendation: is right — don’t screen‑scrape the Translate UI. It’s brittle and will break if Google changes the page; the UI uses AJAX to fetch translations so scraping the initial HTML won’t work reliably. Use an official translation API instead (Google Cloud Translation v3 or a competitor) and authenticate with a service account or API key. The Cloud Translation docs show how to enable the API, authenticate, and call TranslateText from client libraries. (stackoverflow.com)

Minimal .NET pattern (WinForms): install the NuGet package Google.Cloud.Translate.V3, do the API call off the UI thread, then update the label via Invoke. Example:

using Google.Cloud.Translate.V3;

string TranslateText(string projectId, string text, string targetLang = "bn")
{
    var client = TranslationServiceClient.Create();
    var parent = $"projects/{projectId}/locations/global";
    var request = new TranslateTextRequest
    {
        Parent = parent,
        Contents = { text },
        MimeType = "text/plain",
        TargetLanguageCode = targetLang
    };
    var response = client.TranslateText(request);
    return response.Translations.Count > 0 ? response.Translations[0].TranslatedText : string.Empty;
}

Run this from a background task and set label.Invoke(...) to update the UI. See the Cloud docs for authentication (service account / ADC), quotas and example requests. (docs.cloud.google.com)

Alternatives and cautions: Microsoft Translator (Azure) also has C# samples if you prefer Azure, and if you absolutely must scrape a dynamic page use a headless browser (Playwright or Selenium) to render JavaScript — but only for pages you’re allowed to scrape and expect this to be fragile compared to the API approach. (github.com)

Hi,

You cannot use google translate without google "approvel".
You need to open an account and then use their API.

You can find more information here:
https://developers.google.com/translate/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.