Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python webdriver-如何列出所有内容并逐个单击href_Python_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Python webdriver-如何列出所有内容并逐个单击href

Python webdriver-如何列出所有内容并逐个单击href,python,selenium,selenium-webdriver,webdriver,Python,Selenium,Selenium Webdriver,Webdriver,我可以列出所有title=“race1”、“race2”、“race3”,然后从下面的HTML中使用python吗 driver.find_element_by_name('title=Race1').click() 然后一个接一个地点击,这意味着总共点击了3次。第一次单击“比赛1”,第二次单击“比赛2”,第三次单击“比赛3”,谢谢 <table border="0" cellspacing="0" cellpadding="0"> <tbody><tr>

我可以列出所有title=“race1”“race2”、“race3”,然后从下面的HTML中使用python吗

driver.find_element_by_name('title=Race1').click() 
然后一个接一个地点击,这意味着总共点击了3次。第一次单击“比赛1”,第二次单击“比赛2”,第三次单击“比赛3”,谢谢

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

比赛
首先, 您试图单击的元素似乎是
img
。因此,如果您想找到链接本身,可以尝试(通过xpath)获取图像

您可以这样做:

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()
首先,, 您试图单击的元素似乎是
img
。因此,如果您想找到链接本身,可以尝试(通过xpath)获取图像

您可以这样做:

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

驱动程序。按\u名称(“title=Race1”)查找\u元素。\u单击()
无效,因为所需的节点没有名称属性

通过
通过\u id(“raceSelBtn1
)查找\u元素\u访问上述节点的一种方法`

但是,由于您需要一个webelements列表,因此必须使用
find\u elements\u by\u xpath()
方法(复数)。当您使用
find_elements\u by_xxx
时,它会返回与给定位置策略匹配的webelements列表。然后,可以循环此列表以对单个元素执行操作

尝试以下代码片段。

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

驱动程序。按\u名称(“title=Race1”)查找\u元素。\u单击()
无效,因为所需的节点没有名称属性

通过
通过\u id(“raceSelBtn1
)查找\u元素\u访问上述节点的一种方法`

但是,由于您需要一个webelements列表,因此必须使用
find\u elements\u by\u xpath()
方法(复数)。当您使用
find_elements\u by_xxx
时,它会返回与给定位置策略匹配的webelements列表。然后,可以循环此列表以对单个元素执行操作

尝试以下代码片段。

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

阅读更多关于位置策略的信息-->阅读更多关于位置策略的信息-->