So I want to be able to extract the some HTML code from a webpage and assign it to a variable with Excel VBA. Here is my example VBA code:
Pass = IE.Document.getElementsByClassName("summary_field_value easy-read-display")(0).innerText
This returns text, but not the right text from the webpage. In the HTML code, there are a number of fields that look like this:
<div class="ui-body-b summary_field">
<span class="summary_field_name">Username:</span>
<span class="summary_field_value easy-read-display">TestUser</span>
<div class="ui-body-b summary_field">
<span class="summary_field_name">Password:</span>
<span class="summary_field_value easy-read-display">uhQT65$We2</span>
So when my code runs, it produces "TestUser". How can I get it to return "uhQT65$We2" which is the password since the class names are the same (summary_field_value easy-read-display)?
Thanks for the help.
Related
I have a requirement to verify field name and values. My code looks like
<div class="line info">
<div class="unit labelInfo TextMdB">
Reference #:
</div>
<div class="unit lastUnit">
701
</div>
</div>
</div>
<div class="line info">
<div class="unit labelInfo TextMdB">
Registered Date:
</div>
<div class="unit lastUnit">
05/05/2020
</div>
</div>
I gave my xpath as
"//div[#class='unit lastUnit']//preceding-sibling::div[#class='unit labelInfo TextMdB' and contains(text(),'Reference #:')]".
With this xpath I am able to reach "reference#" field . But how to verify reference # field is displaying the value (in this case 701) .
Appreciate your response.
Thanks
You can first reach the Reference # text by using its text in the xpath and then you can use following-sibling to fetch the div tag and then use getText()(java) / text (python) method to get 701.
(Edited answer after OP's comment)
If you want to check if the element is displayed on the page or not then you can fetch its list and check if the size of that list is greater than 0 or not.
You can do it like:
In Java:
List<WebElement> elementList = driver.findElements(By.xpath("//div[#class='line info']//div[contains(text(),'Reference #')]//following-sibling::div"));
if(elementList.size()>0){
// Element is present on the UI
// Finding its text
String text = elementList.get(0).getText();
}
In python:
elementList = driver.find_elements_by_xpath("//div[#class='line info']//div[contains(text(),'Reference #')]//following-sibling::div")
if (elementList.len>0):
# Element is present
# Printing its text
print(elementList[0].text)
I am learning VBA to extract text from website. i want to extract 26/05/2018 14:51 and the innertext did not work properly.
The code from the website:
<label class="wtext">
Refreshed at:
<span id="lastRefreshTime">26/05/2018 14:51</span>
</label>
the below is my code but it does not work out.
Set MDate= html.getElementsByClassName("wtext")(1).getElementById("lastRefreshTime")
MDate1 = Match_Date.innerText
And I had tried the below and it gives me "Refreshed at:"
Set MDate= html.getElementsByClassName("wtext")(1)
MDate1 = Match_Date.innerText
i found some lines in the code about it. That seems is a javascript function. Is there anything still i can do for it?
<script type="text/javascript">
document.getElementById('lastRefreshTime').innerHTML = '26/05/2018 14:51';</script>
Sample code:
<div class="loginbox">some code</div>
<div class="loginbox">other code</div>
<div class="loginbox">
<p> style="color: Red;">Test Extract</p>
</div>
Using Selenium Web Driver, I would like to extract the text Test Extract within the paragraph element which is nested within a div, whose class name is shared with other div classes. c# preferred.
You can try below method:
driver.findElement(By.xpath("//div[#class='loginbox']/p")).getText();
EDITED
You should use = inside the square braces like:
driver.findElement(By.xpath("//div[#class='loginbox']/p");
C# code to get the text from the locator specified,
IWebElement element = Browser.GetElementByCssSelector("div.loginbox p");
string text = element.Text;
How do I get this, from my webbrowser into a label in vb?
Like.. how do do I get the "apple" into a label?
<div class="banana">
<input class="form-control" type="text" placeholder="Result" value="apple>
</div>
Try the following:
YourLabel.Text = Wb.Document.GetElementById("yourId").GetAttribute("value")
You'll have to provide the input tag in the HTML document with an id, though. If you're not in control of the HTML, take a look at GetElementsByTagName.
How can I paste a text to the text area within a form in the browser control?
I think how i have selected is correct
browser1.Document.Forms.GetElementsByName("editform").GetElementsByName("input")
UPDATE:Here is the Html
....
<form name="editform">
<textarea name="input">
</textarea>
</form>
...
Here is an example of how it can be done based on the HTML you've provided. You must first add a reference to MSHTML via the Microsoft.mshtml. Also, I would recommed adding an id attribute to the text area then you can get to it much easier. Something along these lines.
<form name="editform">
<textarea id="myTextArea" name="input">
</textarea>
</form>
Then you can set the value property of the text area.
Dim textArea As HTMLTextAreaElement
textArea = WebBrowser1.Document.GetElementById("myTextArea").DomElement
textArea.value = "Hello World!"
Figured out it's not possible due to security reasons.