使用VBA修改网站Span元素:Pons网站语言切换需求及代码
How to Add Language Switching to Your Pons Website VBA Script
Got it, let's tweak your existing VBA code to include the language switching functionality for the Pons translation site. I'll walk you through the modified code and break down the key parts so you know exactly what's happening.
First, here's what we need to accomplish:
- Load the initial Pons page
- Locate and trigger the language switcher (since the site uses dynamic elements, we'll target the right span/button combo)
- Select your desired language pair
- Proceed with the text translation as before
Here's the updated code (I'll use switching to English → Polish as an example—you can adjust this to your target language pair):
Sub TranslateWithPonsLanguageSwitch() Dim objIE As Object Dim languageSwitchBtn As Object Dim targetLangOption As Object ' Initialize Internet Explorer instance Set objIE = CreateObject("InternetExplorer.Application") With objIE .Top = 0 .Left = 0 .Width = 800 .Height = 600 .AddressBar = 0 .StatusBar = 0 .Toolbar = 0 .Visible = True ' Navigate to the Polish Pons text translation page .Navigate ("https://pl.pons.com/tłumaczenie-tekstu") End With ' Wait for the initial page to fully load Do DoEvents Loop Until objIE.ReadyState = 4 And Not objIE.Busy ' --- Step 1: Open the language switch dropdown --- ' Target the button/span that opens the language selection menu ' Use browser dev tools (F12) to verify the selector if this doesn't work On Error Resume Next Set languageSwitchBtn = objIE.Document.querySelector(".language-pair-selector__button") On Error GoTo 0 If Not languageSwitchBtn Is Nothing Then languageSwitchBtn.Click ' Wait for the dropdown menu to render Do DoEvents Loop Until objIE.Document.querySelectorAll(".language-pair-selector__dropdown").Length > 0 ' --- Step 2: Select your target language pair --- ' Example: Switch to English → Polish (adjust the text to your preferred pair) On Error Resume Next Set targetLangOption = objIE.Document.querySelector(".language-pair-selector__item:contains('Angielski -> Polski')") On Error GoTo 0 If Not targetLangOption Is Nothing Then targetLangOption.Click ' Wait for the page to reload with the new language pair Do DoEvents Loop Until objIE.ReadyState = 4 And Not objIE.Busy Else MsgBox "Target language option not found—check your selector!" objIE.Quit Set objIE = Nothing Exit Sub End If Else MsgBox "Language switcher button not found—verify the HTML selector!" objIE.Quit Set objIE = Nothing Exit Sub End If ' --- Step 3: Input text and trigger translation --- ' Set the text you want to translate objIE.Document.GetElementsByTagName("TEXTAREA")(0).Value = "piłka" ' Click the translation button (adjust the index if the button position changes) objIE.Document.GetElementsByTagName("BUTTON")(5).Click ' Wait for the translation results to load Do DoEvents Loop Until objIE.ReadyState = 4 And Not objIE.Busy ' Optional: Uncomment below to auto-close IE after translation ' objIE.Quit ' Set objIE = Nothing End Sub
Important Notes:
- Element Selectors: Pons might update their HTML structure over time. If the selectors don't work, use your browser's developer tools (F12) to inspect the language switcher elements:
- Right-click the current language pair (e.g., "Polski -> Angielski") and select "Inspect" to find the correct class for the switch button.
- Do the same for your target language option in the dropdown to update the selector.
- Error Handling: The
On Errorblocks prevent the script from crashing if elements aren't found—you can expand these for more robust checks if needed. - Wait Loops: The
DoEventsloops ensure we wait for dynamic elements (like the dropdown) to load before interacting with them, which is critical for sites like Pons that use JavaScript to render content.
内容的提问来源于stack exchange,提问作者Arkadiusz
相关产品推荐
相关产品推荐

