I need to change span element on the website, I mean I have to change a language on the pons website. Here's my code:
Sub www()
End Sub
I need to change span element on the website, I mean I have to change a language on the pons website. Here's my code:
Sub www()
End Sub
In line with my earlier answer to this here is how you change the languages.
Note:
Late binding won't offer you the interface for .querySelector
. It has to be with early binding i.e. reference added for HTML Object Library
.
Additional reference is Microsoft Internet Controls
though this doesn't affect .querySelector
.
To do from and to languages use:
.querySelectorAll("button span")(0).innerText = "bulgarski" '<== To .querySelectorAll("button span")(1).innerText = "arabskiego" '<== From
Or more targeted:
.querySelectorAll("button[class = ""btn dropdown-toggle""]")(0).innerText = "chinskiego" .querySelectorAll("button[class = ""btn dropdown-toggle""]")(1).innerText = "bulgarski"
Or even:
.querySelectorAll("button.btn.dropdown-toggle span")(0).innerText = "chinskiego" .querySelectorAll("button.btn.dropdown-toggle span")(1).innerText = "arabskiego"
The methods above use CSS selectors i.e. that make use of the page styling to select the items of interest.
querySelectorAll
returns a NodeList
of all nodes matching the specified CSS selector. The NodeList
items are then accessed by index e.g. 0, 1.
CSS Selectors:
Code:
Option Explicit Public Sub GetInfo() Dim IE As New InternetExplorer, html As HTMLDocument, translation As String Const TRANSLATION_STRING As String = "Hello" With IE .Visible = True .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu" While .Busy Or .readyState < 4: DoEvents: Wend Set html = .document With html .querySelectorAll("button span")(0).innerText = "polskiego" .querySelectorAll("button span")(1).innerText = "angielskiego" ' .querySelectorAll("button[class = ""btn dropdown-toggle""]")(0).innerText = "chinskiego" ' .querySelectorAll("button[class = ""btn dropdown-toggle""]")(1).innerText = "bulgarski" ' ' .querySelectorAll("button span")(0).innerText = "chinskiego" ' .querySelectorAll("button span")(1).innerText = "chinskiego" '' ' .querySelectorAll("button.btn.dropdown-toggle span")(0).innerText = "chinskiego" ' .querySelectorAll("button.btn.dropdown-toggle span")(1).innerText = "arabskiego" Stop .querySelector("textarea.text-translation-source.source").Value = TRANSLATION_STRING .querySelector("button.btn.btn-primary.submit").Click Application.Wait Now + TimeSerial(0, 0, 3) translation = .querySelector("div.translated_text").innerText End With Debug.Print translation 'Quit '<== Remember to quit application End With End Sub
Example run:
Edit:
This is not robust but you may be able to do, for late binding with HTML file
.getElementsByTagName("span")(0).innerText = "chinskiego" '<== from .getElementsByTagName("span")(26).innerText = "bulgarski" '<== to
Late bound:
Option Explicit Public Sub GetInfo() Dim IE As Object, html As Object With CreateObject("InternetExplorer.Application") .Visible = True .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu" While .Busy Or .readyState < 4: DoEvents: Wend Set html = CreateObject("htmlfile") Set html = .document With html .getElementsByTagName("span")(0).innerText = "chinskiego" .getElementsByTagName("span")(26).innerText = "bulgarski" Stop End With .Quit End With End Sub
Don't really want this answer to go on forever (so apologies for current length)... In response to OPs later question regarding sometimes values not held in late bound translation.
Please see more fragile version. In the version below I have first looped all the span elements and found out which index relates to which language selection and in which box:
Mini guide to spans:
Code:
Option Explicit Public Sub GetInfo() Dim IE As Object, html As Object, translation As String With CreateObject("InternetExplorer.Application") .Visible = True .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu" While .Busy Or .readyState < 4: DoEvents: Wend Set html = CreateObject("htmlfile") Set html = .document Dim i As Long 'Dim listOfSpanElements As Worksheet 'Set listOfSpanElements = Worksheets.Add ' With listOfSpanElements ''<== This was used to ascertain position of all the span elements and hence which span to click on to select a language. ' ' For i = 0 To html.getElementsByTagName("span").Length - 1 ' Cells(i + 1, 1) = html.getElementsByTagName("span")(i).innerText ' Next i ' ' End With With html .getElementsByTagName("button")(1).Focus .getElementsByTagName("button")(1).Click .getElementsByClassName("text-translation-source source")(0).innerText = "Sponsorowane" .getElementsByTagName("span")(15).Click 'FROM polskiego .getElementsByTagName("span")(47).Click 'TO angielski .getElementsByClassName("btn btn-primary submit")(0).Click Application.Wait Now + TimeSerial(0, 0, 4) Stop For i = 0 To .getElementsByClassName("text-translation-target target").Length - 1 Debug.Print .getElementsByClassName("text-translation-target target")(i).innerText '<==later remove "Trwa ladowanie..." Next i End With .Quit End With End Sub