0

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>

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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()

2 Comments

:does it not click the same link when we follow the above method.please correct me if i am wrong
@saranya I had a typo with the window_handles... [1] is should be [-1]...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.