0

My current code opens a website and logins. the next step is that I want to select a link on the next page but don't know how to get the code to click on it (first time messing with HTML code). below is a picture of the HTML behind that link. If more info is needed I can update with more pictures.

enter image description here

<td align="left" style="vertical-align: top;"> <div class="gwt-Hyperlink">PRC search by selection</div> </td> 

My Excel code:

Sub Login() Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ' ie.navigate "http://sqa.subaru-sia.com/nxps/nxps?action=defaultPage" ' Modify the URL here.... Const Url$ = "http://sqa.subaru-sia.com/nxps/nxps?action=defaultPage" Dim UserName As String, Password As String, LoginData As Worksheet UserName = "xxxxxxx" Password = "xxxxxxx" With ie .navigate Url ieBusy ie .Visible = True Dim oLogin As Object, oPassword As Object Set oLogin = .document.getElementsByName("j_username")(0) Set oPassword = .document.getElementsByName("j_password")(0) oLogin.Value = UserName oPassword.Value = Password .document.forms(0).submit End With Do While ie.Busy Application.Wait DateAdd("s", 1, Now) Loop Set myColl = ie.document.getElementsByTagName("div") For Each myItm In myColl Cells(i + 1, 1) = "Table# " & ti + 1 ti = ti + 1: i = i + 1 For Each trtr In myItm.Rows For Each tdtd In trtr.Cells 'Debug.Print (tdtd.innerText) If tdtd.innerText = "PRC search by selection" Then tdtd.Children(0).Click End If j = j + 1 Next tdtd i = i + 1: j = 0 DoEvents Next trtr i = i + 1 Next myItm 'ie.Quit End Sub Sub ieBusy(ie As Object) Do While ie.Busy Or ie.readyState < 4 DoEvents Loop End Sub 

1 Answer 1

2

From the description, it looks like you want to click the link with the text PRC search by selection on the page using IE VBA Automation.

If we try to check the HTML code in the image then we can notice that the page contains nested tables and what you are assuming as a link is actually a DIV tag. So I am assuming that you want to click the DIV.

I tried to make a test webpage with the nested tables and a similar kind of DIV and try to click the DIV using the code below.

Sub demo() Dim ie Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.navigate "https://Your_website_here..." ' Modify the URL here.... Do While ie.Busy Application.Wait DateAdd("s", 1, Now) Loop Set myColl = ie.document.getElementsByTagName("TABLE") For Each myItm In myColl Cells(i + 1, 1) = "Table# " & ti + 1 ti = ti + 1: i = i + 1 For Each trtr In myItm.Rows For Each tdtd In trtr.Cells 'Debug.Print (tdtd.innerText) If tdtd.innerText = "PRC search by selection" Then tdtd.Children(0).Click End If j = j + 1 Next tdtd i = i + 1: j = 0 DoEvents Next trtr i = i + 1 Next myItm 'ie.Quit End Sub 

Output:

enter image description here

Further, you can try to modify the code as per your own requirements.

Sign up to request clarification or add additional context in comments.

7 Comments

I get Run-Time error '438' Object doesn't support this property or method. on For each trtr In myItm.Rows I will update my question with the full length of code. The first part of the code gets me past the login screen onto the screen with the div that I need to select.
I am not sure why you had tried to get the DIV instead of Table in this line Set myColl = ie.document.getElementsByTagName("div"). You need to get all Table elements. Further, try to check whether ie.document is properly assigned after moving to the next page. If not, you can again try to assign it.
When I run the code the Immediate window says "Loading. Please wait..." I think this is cause the code is running faster than IE can keep up. Also even when it finds the tdtd.innertext that I want to be select it doesn't select it.
If you think the code execution is faster than IE then try to wait for some seconds using Application.Wait DateAdd("s", 1, Now). I suggest you debug the VBA code step by step by pressing the F8 key. It can help to narrow down the issue.
From your last comment, it looks like your issue is solved now. If the suggestion is helpful, I suggest you accept it as an answer to this question. It can help other community members in the future with similar kinds of questions. Please see here. Thanks for your understanding.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.