我想使用python的selenium驱动程序点击下面的html中的第二个链接。但我无法弄清楚如何制作它。
<div class="outerResults" style="">
<div class="results searchResults" style="display: block;">
<table cellspacing="0" id="resultGroup">
<colgroup>
</colgroup>
<tbody>
<tr>
<td class="selector">
<a href="#" id="p_-1454220127" title="Gary Smith" class="checkbox unchecked"> </a>
</td>
<td class="personName">
<a href="#!search/profile/person?personId=-1454220127&targetid=profile">
Gary Smith
</a>
</td>
<td class="title">
Chief Executive Officer and Co-Founder
<input type="hidden" name="" value="Chief Executive Officer and Co-Founder" id="personTitle">
<br>
<div class="companyLink">
<br>
<a href="#!search/profile/company?companyId=104333869&targetid=profile" class="">
Tesla Motors Inc
</a>
</div>
</td>
<td class="contact">
<span class="unmaskContactText">Click to view this profile</span>
<span class="detailContactMask">Email</span>
<span class="upsellContainer" rel="upsellContactInfo"> </span><br>
<div class="phoneNumber">
<span class="detailContactMask">Phone</span>
</div>
</td>
<td class="date">
<span class="date" title="Last Update">
5/21/14
</span>
</td>
</tr>
<tr>
<td class="selector">
<a href="#" id="p_-1832984054" title="Tesla EVs" class="checkbox unchecked"> </a>
</td>
<td class="personName">
<a href="#!search/profile/person?personId=-1832984054&targetid=profile">
Tesla EVs
</a>
</td>
<td class="title">
Chief Executive Officer
<input type="hidden" name="" value="Chief Executive Officer" id="personTitle">
<br>
<div class="companyLink">
<br>
<a href="#!search/profile/company?companyId=104333869&targetid=profile" class="">
Tesla Motors Inc
</a>
</div>
</td>
<td class="contact">
<span class="unmaskContactText">Click to view this profile</span>
<span class="detailContactMask">Email</span>
<span class="upsellContainer" rel="upsellContactInfo"> </span><br>
<div class="phoneNumber">
<span class="detailContactMask">Phone</span>
</div>
</td>
<td class="date">
<span class="date" title="Last Update">
6/24/15
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
目的是点击<td class="personName">
的文字,例如Gary Smith
和Tesla EVs
。我尝试使用以下代码点击Gary Smith
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, "personName"))).click()
但是什么都没发生。而且我也不知道如何使用selenium驱动程序点击Tesla EVs
我想说,使用不同的输入,内容名称会动态更改。所以准确地说,我想点击第一个和第二个链接,而内部内容名称是随机的
任何能够给我提示的人都将受到高度赞赏。
答案 0 :(得分:1)
点击Gary Smith
链接尝试:
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH, "//a[.='Gary Smith']"))).click()
或
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.LINK_TEXT, 'Gary Smith'))).click()
Tesla EVs
相同:
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH, "//a[.='Tesla EVs']"))).click()
或
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.LINK_TEXT, "Tesla EVs"))).click()
如果链接文本不是常量,请尝试使用以下选择器:
# For 'Gary Smith'
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "tr:nth-child(1) > td.personName > a"))).click()
# For 'Tesla EVs'
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "tr:nth-child(2) > td.personName > a"))).click()