saranya
saranya

Reputation: 163

how to iterate through the link in selenium/python

In web page i have n number of links ie the links displayed are random there may be one link or there be five links.I have written code in way that it will open first link in a new tab and do some function.The problem is i don't how to do the same if there are more links . is it possible to iterate or any other way to handle this

Changerequest is the link that is present in the webpage

Sourcecontrol = driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
   Sourcecontrol.click();
   Changerequest=driver.find_element_by_xpath( '//td[@class="confluenceTd"]/a[contains(.,"Change: ")]');
   testvalue = Changerequest.get_attribute('href')
   driver.execute_script("window.open(arguments[0])",testvalue)
   window_after = driver.window_handles[1]
   driver.switch_to_window(window_after)

sample HTML of the links

<div class="flooded">
                    <div class="table-wrap">
<table class="confluenceTable"><tbody>
<tr>
<td class="confluenceTd"><a href="link" class="external-link" rel="nofollow" title="Follow link">Change: 1111</a></td>
<td class="confluenceTd">date</td>
</tr>
<tr>
<td class="confluenceTd"><a href="Link" rel="nofollow" title="Follow link">Change: 2222</a></td>
<td class="confluenceTd">date</td>
</tr>
<tr>
<td class="confluenceTd"><a href="link" class="external-link" rel="nofollow" title="Follow link">Change: 33333</a></td>
<td class="confluenceTd">date</td>
</tr>
<tr>
<td class="confluenceTd"><a href="link" class="external-link" rel="nofollow" title="Follow link">Change: 44444</a></td>
<td class="confluenceTd">date</td>
</tr>
</tbody></table>
</div>

                </div>

Upvotes: 0

Views: 164

Answers (2)

Moshe Slavin
Moshe Slavin

Reputation: 5204

You can use find_elements with s to get all the elements...

Your loop should look something like:

for i in driver.find_elements_by_xpath( '//td[@class="confluenceTd"]/a'):
    testvalue = i.get_attribute('href')
    driver.execute_script("window.open(arguments[0])",testvalue)
    window_after = driver.window_handles[-1]
    driver.switch_to_window(window_after)
    # do something...
    driver.switch_to.default_content()

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Try the following code.

from selenium import webdriver
driver=webdriver.Chrome()
driver.get("url")
for item in driver.find_elements_by_css_selector('td.confluenceTd a'):
    link = item.get_attribute('href')
    window_before = driver.window_handles[0]
    driver.execute_script("window.open(arguments[0])",link)
    window_after = driver.window_handles[-1]
    driver.switch_to.window(window_after)
    #Perform some action
    #
    print(driver.current_url)
    #
    driver.switch_to.window(window_before)

OR

for item in driver.find_elements_by_xpath("//td[@class='confluenceTd']//a[contains(.,'Change:')]"):
    link = item.get_attribute('href')
    window_before = driver.window_handles[0]
    driver.execute_script("window.open(arguments[0])",link)
    window_after = driver.window_handles[-1]
    driver.switch_to.window(window_after)
    #Perform some action
    #
    print(driver.current_url)
    #
    driver.switch_to.window(window_before)

Upvotes: 1

Related Questions