0

I'm trying to click a link on a website with the tag:

<a href="/dbget-bin/www_bget?dr:D01441:>D01441</a>

However, I'm doing this after searching for a unique item (I have an array of >9000 unique items), and the "D01441" part is different for each item, and I don't know in advance what it will be for each. The following code is in a loop that goes through each item and searches for it one at a time. After searching, I would like to click on a link that appears (the code above) and do more things on that next web page.

Dim IE As Object
Dim ele As Object
Set IE = CreateObject("InternetExplorer.Application")

...

For Each ele In IE.document.getElementsByTagName("a")
    If ele.Href = "/dbget-bin/www_bget?dr:D01441" Then
        ele.Click
        Exit For
    End If
Next

The above code doesn't work and I'm not sure why. But once I get it to work, I don't know how to modify the "D01441" part so that I can click on any searched item's link. Here's more html around the link I want:

<tbody>
  <tr> ... </tr>
  <tr>
    <td class = "data1">
      <a href = "/dbget-bin/www_bget?dr:D01441:>D01441</a>
    </td>
    <td class = "data1">..</td>
    <td class = "data1">..</td>

...

EDIT: To try to deal with the changing "D01441", I tried using InStr but it doesn't work either:

    For Each ele In IE.document.getElementsByTagName("a")
        If InStr(ele.Href, "/dbget-bin/www_bget?dr:") = 1 Then
            MsgBox "There"
            ele.Click
            Exit For
        End If
    Next
3
  • I believe you want the .innerText property of ele (not .Href), which should be equal to D01441 in your example. Commented Aug 8, 2018 at 14:16
  • @dwirony I'm not sure how to incorporate .innerText, because don't I still need to find the right tag by looking at .href? Commented Aug 8, 2018 at 14:22
  • If i use .innerText then wouldn't I need to hardcode "D01441"? I need it to be whatever is the correct link on the page Commented Aug 8, 2018 at 14:32

2 Answers 2

3

CSS selectors:

Try using a CSS selector combination applied via querySelector method of document to target the common start part of the href.


Applying the selector combination:

IE.document.querySelector("a[href^='/dbget-bin/www_bget?dr:']").Click

Understanding the selector combination:

This uses a CSS selector combination to target the element with:

a[href^='/dbget-bin/www_bget?dr:']

This says element with a tag having attribute href whose value starts with '/dbget-bin/www_bget?dr:' . The ^ means starts with.


Query in action:

Here is the selector in action on your HTML sample:

sample


Side note:

If you have multiple elements with a tags and an href that starts with /dbget-bin/www_bget?dr:, it will match the first one, in most instances. If that is the case seeing more HTML would help. I think there are a few problems with that HTML sample because in theory a more selective CSS query might be .data1 a[href^='/dbget-bin/www_bget?dr:'], so as to include the parent element class of data1, "." being a class selector.

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

3 Comments

Oh i'd never seen that before - thank you it works!! I realized some search queries don't have any results, in that case, is there a way to say If IE.document.querySelector("...") not on page?
Nvm you can just check if it = 0
Another awesome approach.
2

@QHarr answer is the elegant and best solution, but...

To address your issue of getting the part number from the href, you can use the InStr like this

For Each ele In IE.document.getElementsByTagName("a")
    Dim partNumber As String
    Dim colonPosition As Long
    colonPosition = InStr(1, ele.Href, ":", vbTextCompare)
    If colonPosition > 0 Then
        partNumber = Right$(ele.Href, Len(ele.Href) - colonPosition)
        Debug.Print partNumber
    End If
Next ele

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.