3

Quick Links

            <span class="buttonContainer"><input type="image" class="helpButton" src="/images/buttons/btn_help.gif" alt="View  help." title="View  help."><input type="image" class="minimizeButton" src="/businesscare/images/dashboard/btn_minus_white.gif" alt="Show less." aria-controls="tile-contents" aria-expanded="true" aria-live="polite"></span></dt>
            <dd>
                <strong>Help:</strong> Quickly access the tools you need to view your 
                             Name lists.
                        </dd>
        </dl>

        <div class="tile-contents">
            <div><strong>Note:</strong> links open a new window.</div>
            <div class="loading" style="display: none;">
                <div>
                    <p></p>
                </div>
            </div>
            <hr>
            <div class="configuration" style="display: none">
                <p>Choose and drag any transaction type to reorder your
                    shortcuts. Your top five shortcuts will be displayed by default in
                    the shaded area.</p>
                <hr>
                <ul class="dragable">
                </ul>                   
            </div>

            <h4>Links</h4>  
                    <ul class="contents">                       
                        <li><a href="Home" title="View home" onclick="openlink(this,this.title);return false;">View home</a></li>
                    </ul>
                    <hr>
                    <h4 id="Numbers">Numbers</h4>
                    <ul class="contents">                           
                        <li><a href="Link1" onclick="openlink(this,this.title);return false;">View Link1</a></li>
                        <li><a href="Link2" title="View Link2" onclick="openlink(this,this.title);return false;">View Link2</a></li>
                        <li><a href="Link3" title="View Link4" onclick="openlink(this,this.title);return false;">View Link3</a></li>
                    </ul>
                    <hr>

                    <h4 id="Photos">Photos</h4>                         
                    <ul class="contents">
                            <li><a href="Link4" title="Select Link4" onclick="openlink(this,this.title);return false;">Select Link4</a></li>
                        </ul>
                    <hr>

                    <h4 id="downloads">Downloads</h4>
                    <ul class="contents">
                        <li><a href="Link5" title="Download Link5" onclick="openlink(this,this.title);return false;">Download </a></li>
                    </ul>
                    <hr>

                    <h4 id="qlinkusers">Users</h4>
                    <ul class="contents">
                        <li><a href=Link6" title="View Link6" onclick="openlink(this,this.title);return false;">View Users</a></li>
                    </ul>                       
                </div>
    </li>

I need to fetch all the hrefs on the page and click them. I tried to fetch the links using the following code but the code returns empty list.

hRefs = []

parent = browser.find_element_by_class_name("contents")

links = parent.find_elements_by_tag_name("a")
for link in links:
    hRefs.append(link)
print hRefs

This is what I get when I run the code an empty list.

[<selenium.webdriver.remote.webelement.WebElement (session="386157b412d34905ae7344a206c156da", element="0.8337721961119238-2")>]
1

It does not return an empty list, it returns a list with a single <WebElement>.

parent = browser.find_element_by_class_name("contents")

Only gives back the first content block, the provided HTML also shows it only has a single <a> tag. So the result is correct :)

Try this code to get all a_elements

a_elements = []

content_blocks = browser.find_elements_by_class_name("contents")

for block in content_blocks:
    elements = block.find_elements_by_tag_name("a")
    for el in elements:
        a_elements.append(el)

print a_elements

This should show all <a> elements in each content block. Still it wont show the hrefs, if you want those replace append(element) with append(element.get_attribute("href"))

list_of_hrefs = []

content_blocks = browser.find_elements_by_class_name("contents")

for block in content_blocks:
    elements = block.find_elements_by_tag_name("a")
    for el in elements:
        list_of_hrefs.append(el.get_attribute("href"))

print list_of_hrefs

Probably it is better to find with a CSS selector so you do not have to loop twice.

   elements = browser.find_elements_by_css_selector(".contents > a")

This should return all <a> elements under elements with a class .contents

  • Thanks for your reply @Neils van Reijmesdal. The code was useful. I accepted your answer, can't up vote because I am a new to this forum. The only problem that I see is there is a u prefixed to each element of the list, although when I loop over the list_of_hrefs and print the hrefs, the links text is displayed. – Dev Bin Aug 24 '18 at 14:30
  • I used str(href) and I don't see the unicode character now. But still there is one last thing, in the list of hrefs, three hrefs are clubbed as one with a "\n" character. When I iterate over the list to click each href, I get an error for the above mentioned list items that have \n. ['Link1', 'Link2\nLink3\nLink4', 'Link5', 'Link6'] – Dev Bin Aug 24 '18 at 16:02
  • That must be another page, cause it is missing the Home link. You can check if the string contains a \n and split() it and them. docs.python.org/2/library/string.html – Niels van Reijmersdal Aug 24 '18 at 17:37
-1

I was able to fetch the links using @Niels-van-Reijmersdal code and was able to click the hrefs using the text.

hRefs = []

parent = browser.find_elements_by_class_name("contents")
for link in parent:
    links = link.find_elements_by_tag_name('a')
    for l in links:
        hRefs.append(str(l.text))
        browser.find_element_by_link_text(l.text).click()   
print hRefs
  • You can also just click on the element it self with l.click() no need to get the text first. – Niels van Reijmersdal Aug 24 '18 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.