Element doesn't exist although it has ID attribute - excel

In selenium excel vba I am trying to learn more about how to deal with the CSS selectors
And I am wondering because when inspecting an element with ID and when run the code I got a message that the element not found
Here's the code till now
Private bot As New selenium.ChromeDriver
Sub Test()
Dim win, mainWin As selenium.Window, sCode As String, i As Long
Dim urlImage As String, urlPost As String
Dim sCase As String
sCase = "192160470"
Set bot = New ChromeDriver
With bot
.Start "Chrome"
'First Window (Main Window)
.Get "https://www.kuwaitcourts.gov.kw/searchPages/searchCases.jsp"
'.FindElementById("txtCaseNo").SendKeys sCase
.FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase
'MsgBox "Click OK After Entering Captcha", 64
Stop
.Quit
End With
End Sub
and here's the HTML part for that element
<td><input type="text" name="txtCaseNo" id="txtCaseNo" maxlength="9" class="inputTextBox" onkeypress="return onlyNumbers(event);"></td>
I am stuck at this line
.FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase
Thanks advanced for any help or any ideas

To send a character sequence to the Username field as the the desired element are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
With bot
.Start "Chrome"
.Get "https://www.kuwaitcourts.gov.kw/searchPages/searchCases.jsp"
.SwitchToFrame "searchCaseDiv"
.FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase
You can find a relevant discussion in How to send text with in the username field within an iframe using Selenium VBA
tl; dr
Ways to deal with #document under iframe

The element is inside of an iframe with id searchCaseDiv. You have to switch to that iframe to be able to access the element.
Use .SwitchToFrame to switch frame.
For java, it would be like this,
driver.switchTo().frame("searchCaseDiv");

Related

Trying to fill in data and click on button on Chrome browser after opening URL

I'm a using VBA Excel to try and open a URL via Chrome to fill in data and hit a submit button.
I'm able to write the command to open the URL but I am not sure how to continue to key in the data and submit the form
My Code starts as
Sub OpenHyperlinkInChrome()
Dim chromeFileLocation As String
Dim hyperlink As String
hyperlink = "<URL OVER HERE>"
chromeFileLocation = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromeFileLocation & "-url " & hyperlink)
driver.FindElementByName("trackingId").Value = "123"
driver.FindElementByID("epm-submit-button-announce").Click
End Sub
I get a syntax error on the "driver.FindElementByName."
My field HTML code reads as
<input type="text" placeholder="Example: BLUE_SHOES_08. This information is for your tracking purposes." name="trackingId" class="a-input-text a-span12">
The button HTML code reads as
<span id="epm-submit-button-announce" class="a-button-text a-text-center" aria-hidden="true">
Submit
</span>
How can I go about filling the form and submitting?
The problem is that you are calling the "driver" object without defining it or even declaring it.
From the code it looks like you are trying to use Selenium, have you installed Selenium?
After you install Selenium your code should look like this:
Set driver = CreateObject("Selenium.ChromeDriver")
driver.start "chrome"
driver.Get "<URL OVER HERE>"
driver.FindElementByName("trackingId").SendKeys ("123")
driver.FindElementByID("epm-submit-button-announce").Click

Web scraping DEEPL.com using VBA Excel and Selenium

i'm trying to code a function to translate sentences in Excel using DEEPL.com
My approach is using Selenium to scrape the web using Chrome (as IExplore is not supported by the web).
Public Function deepL(txt As String, inputLang As String, outputLang As String)
Dim url As String
Dim driver As New WebDriver
url = "https://www.deepl.com/translator#" & inputLang & "/" & outputLang & "/" & txt
driver.Start "Chrome"
driver.Timeouts.ImplicitWait = 5000
driver.Get url
deepL = driver.FindElementById("target-dummydiv").Text
driver.Close
End Function
----
Sub translating()
'test for word "probando" from "es" to "en"
'url: https://www.deepl.com/translator#es/en/probando
'it should return: "testing"
MsgBox (deepL("probando", "es", "en"))
End Sub
The problem comes when loading the web, so the div containing the translation is empty on load, and the GET instruction returns an empty text.
But after 1 second, the page refreshes with the correct result:
<div id="target-dummydiv" aria-hidden="true" class="lmt__textarea lmt__textarea_dummydiv" lang="en-US">testing</div>
I tried adding an implicit wait of 5 seconds in order to give time to the webpage to load, but the result is the same.
What am I doing wrong?
EDIT: I found that the div with the translation has visibility: hidden. If I show the visibility, the results are correct, but don't know how to get that in my code
OK, I found a solution:
just select the textarea where the translation is located and get the translation with .attribute("value") instead of .text
deepL = driver.FindElementByCss("textarea.lmt__textarea.lmt__target_textarea.lmt__textarea_base_style").Attribute("value")

readonly cells ... .. ......

Code is required
which type of value format is needed to enter in Excel cell, with one example
these are the coding stuff, also provided you an image where any can visualize how the problem look like. And in this problem we can't just use .SendKeys here it is more typical, because it have the Date-Month-Time, so help me out in this.
I tried, after removing "readonly" word in HTML .. then its working fine, but this is not the way can you edit in this code,
Sub google_search()
Dim row As Integer
row = 2
Dim bot As WebDriver
Set bot = New WebDriver
Dim GenderDD As Selenium.WebElement
bot.Start "chrome"
bot.Get "https://abcd.com/"
bot.FindElementbyName("sample_cdate").SendKeys "Value"
Stop
End Function
Also giving Inspect of Targeted Site, for the reference
<input type="text" class="form-control datetimepicker" name="sample_cdate" id="sample_cdate" placeholder="Date and Time of Sample Collection" **readonly**="">
I tried, after removing "readonly" word in HTML .. then its working
fine, but this is not the way can you edit in this code
You should replace .SendKeys() method:
'bot.FindElementbyName("patient_id").SendKeys Sheet1.Cells(row, 3).Value
bot.ExecuteScript "arguments[0].setAttribute('value', arguments[1])", _
Array(bot.FindElementById("sample_cdate"), _
Format(Sheet1.Cells(row, 16).Value, "yyyy-mm-ddThh:mm:ss"))
As a readonly element, similar as on graphic WebBrowser, you cannot type input using .SendKeys(), but you can use JavaScript to set .Value attribute through programming.
As you show, your input id may be id="sample_rdate", not sample_cdate.

excel vba - Selenium Find Element

I'm using Selenium to do some webscraping.
I always struggle with finding elements when Name or ID isn't an options.
The page is
https://www.portfolio123.com/holdings.jsp?portid=1637063
And I'm trying to find the log in button.
the element looks like:
I've tried
FindElementByClass("btn-primary btn-sm", 10000)
FindElementsByLinkText("Log In", 10000)
FindElementByXPath("//div[#class='btn btn-primary btn-sm']//[#href='javascript:void(goToLoginPage())")
But without any success (I suspect I need to use XPath, but I can't seem to get it right
Any help please? (and if you could explain how to figure this out, so I don't have the issues in future that would be really appreciated)
To locate the element Log In you can use either of the following Locator Strategies:
Using FindElementByLinkText:
FindElementByLinkText("Log In")
Using FindElementByCss:
FindElementByCss("a[href*='goToLoginPage']")
Using FindElementByXPath:
FindElementByXPath("//a[contains(#href, 'goToLoginPage')]")
I tested. try this code below code. its working for me..
Sub loginCode()
Dim bot As New WebDriver
bot.start "chrome", "https://www.portfolio123.com/holdings.jsp?portid=1637063"
bot.Get "/"
Call WaitForSec(5)
bot.FindElementByXPath("//*[#id=""wrapper""]/div/div/div/a[1]").Click
End Sub
Sub WaitForSec(Sec As Integer)
Dim lngTime As Long
lngTime = Timer
While Timer < lngTime + Sec
DoEvents
Wend
End Sub

GetAttribute in Selenium VBA for style

I am working on selenium in VBA and I have stored a variable"post" to store all the occurrences of a specific element like that
Dim post As Object
Set post = .FindElementsByCss("#DetailSection1")
Dim i As Long
For i = 1 To post.Count
Debug.Print post.Item(i).getAttribute("style")
Next i
I need to extract the style value from the elements
<div id="DetailSection1" style="z-index:3;clip:rect(0px,746px,32px,0px);top:228px;left:0px;width:746px;height:32px;">
</div>
Also I need to print in the immediate window the innerHTML and when I used getAttribute("innerHTML"), it doesn't work for me
Any ideas
getAttribute("style") should work but you have to induce a waiter for the element to be present/visible within the HTML DOM.
Debug.Print post.Item(i).getAttribute("style")
Precisely, to extract value of the style attributes from the elements you can use the getCssValue() method as follows:
Debug.Print post.Item(i).getCssValue("z-index")
Debug.Print post.Item(i).getCssValue("top")
Debug.Print post.Item(i).getCssValue("left")
Debug.Print post.Item(i).getCssValue("width")
Debug.Print post.Item(i).getCssValue("height")
getAttribute("innerHTML")
get_attribute("innerHTML") can be used to read the innerHTML or the text within any node / WebElement
You can find a detailed discussion in Difference between text and innerHTML using Selenium
References
You can find a couple of relevant discussions in:
How to get child property value of a element property using selenium webdriver, NUnit and C#
How can I verify text is bold using selenium on an angular website with C#

Resources