Mary
Mary

Reputation: 797

webdriver - how to list all and click the href one by one

Can i list out all title="Race 1", "Race 2", "Race 3" then using python from the folllwing HTML

driver.find_element_by_name('title=Race1').click() 

And then Click one by one, that mean total click 3 time. first click "Race 1" , second click "Race 2" and third click "Race 3" , thanks !

<table border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<td style="padding-right:3px;">Race</td>
<td class="raceButton" style="PADDING-LEFT:3px"><a href="/racing/pages/odds_wp.aspx?lang=EN&amp;date=06-06-2018&amp;venue=HV&amp;raceno=1" onmouseout="MM_swapImgRestore();" onmouseover="MM_swapImage('race_num_1', '', '/racing/info/images/num_1_on.gif?CV=L209R1d',1);"><img id="raceSelBtn1" src="/racing/info/images/num_1_on.gif?CV=L209R1d" border="0" title="Race 1"></a></td>
<td class="raceButton" style="PADDING-LEFT:3px"><a href="/racing/pages/odds_wp.aspx?lang=EN&amp;date=06-06-2018&amp;venue=HV&amp;raceno=2" onmouseout="MM_swapImgRestore();" onmouseover="MM_swapImage('race_num_2', '', '/racing/info/images/num_2_on.gif?CV=L209R1d',1);"><img id="raceSelBtn2" src="/racing/info/images/num_2.gif?CV=L209R1d" border="0" title="Race 2"></a></td>
<td class="raceButton" style="PADDING-LEFT:3px"><a href="/racing/pages/odds_wp.aspx?lang=EN&amp;date=06-06-2018&amp;venue=HV&amp;raceno=3" onmouseout="MM_swapImgRestore();" onmouseover="MM_swapImage('race_num_3', '', '/racing/info/images/num_3_on.gif?CV=L209R1d',1);"><img id="raceSelBtn3" src="/racing/info/images/num_3.gif?CV=L209R1d" border="0" title="Race 3"></a></td>
</tr>
</tbody></table>

Upvotes: 0

Views: 124

Answers (2)

GPT14
GPT14

Reputation: 819

<img id="raceSelBtn1" src="/racing/info/images/num_1_on.gif?CV=L209R1d" border="0" title="Race 1">

driver.find_element_by_name('title=Race1').click() wont work because the node that you want has no name attribute!

One of the way you can access the above node by find_element_by_id('raceSelBtn1)`

But since you want a list of webelements you will have to use find_elements_by_xpath() method(plural). When you use find_elements_by_xxx it returns you a list of webelements which match your given location strategy. You can then loop over this list to perform actions over individual elements.

Try the following code snippet.

buttons_list = driver.find_elements_by_xpath("//img[contains(@title, 'Race')]")
for button in buttons_list:
    button.click()

Upvotes: 2

RobinFrcd
RobinFrcd

Reputation: 5426

First, It seems that the elements you're trying to click on are img. So if you want to find the link itself you can try to get images parents (by xpath).

You can do something like this:

for i in range(1,4):
    img_btn = driver.find_element_by_id('raceSelBtn{}'.format(i))
    btn_link = img_btn.find_element_by_xpath('..') # Get img parent, the link
    btn_link.click()

Upvotes: 1

Related Questions