'Get multiline text from textarea with Selenium VBA
I need to get multiline text from textarea element of web-page. I see in Chrome developer tool that text I need is stored in value property of textarea. I marked it on screenshot
Below is fragment of page source:
<div id="team1">
<div id="lineupsPanel1" style="display: block;">
<textarea id="lineupsField1" placeholder="Enter text for analyze here"></textarea>
<button onclick="viewLineupsInTable(1,1)" title="Process text" style="cursor: pointer; font-weight: bold; color: red; padding: 3px 6px; margin: 0px 4px
0px 0px;">Process</button>
I try below code to get text from value property
Set WebElemTextBox = Chrome.FindElementById("lineupsField1")
If Not (WebElemTextBox Is Nothing) Then
TextVar = WebElemTextBox.value
End If
But so TextVar gets only first line of stored text (on screenshot examplevalue: "Next items are not found:↵↵Pam-pam↵↵Next items are found:↵↵Monitor"
But TextVar gets only "Next items are not found:"
).
How to get full multiline text?
I also found some answers to use something likeTextVar = WebElemTextBox.getAttribute("value")
But I get
"Run-time error: 438. Object doesn't support this property or method"
on that string while testing.
UPDATE. I figured out the problem myself:
When you watch value of the variable by moving cursor to it - then for string variable is showing only first line of text without any mark (three dots or something else). We should use Watch Window instead to not to be cheated.
So method TextVar = WebElemTextBox.value
works well.
Solution 1:[1]
I figured out the problem myself:
When you watch value of the variable by moving cursor to it - then for string variable is showing only first line of text without any mark (three dots or something else). We should use Watch Window instead to not to be cheated. So method
TextVar = WebElemTextBox.value
works well.
Solution 2:[2]
If possible posting the HTML would be helpful, but I'll try answering it anyway.
First Check your element Id for each line you wish to make into a string, I'm assuming they are sequential. Something like "lineupsField1", "lineupsField2", lineupsField3" etc.
If so you can loop through and append these values to a string. Ex:
For i = 1 to 10
Set WebElemTextBox = Chrome.FindElementById("lineupsField" & i)
If Not (WebElemTextBox Is Nothing) Then
TextVar = TextVar & WebElemTextBox.value
End If
Next
If the element Id's are not in sequential order than instead of looping just append till you reach your goal. Ex:
Set WebElemTextBox = Chrome.FindElementById("lineupsField1")
If Not (WebElemTextBox Is Nothing) Then
TextVar = TextVar & WebElemTextBox.value
End If
Set WebElemTextBox = Chrome.FindElementById("Element2")
If Not (WebElemTextBox Is Nothing) Then
TextVar = TextVar & WebElemTextBox.value
End If
As far as the other method you showed:
TextVar = WebElement.getAttribute("value")
I wouldn't recommend going down this path as you are likely going to create more work for yourself parsing through all the values in the HTML.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | VasVovec |
Solution 2 | Blake Daniel |