gpt4 book ai didi

jquery - 如何在 scrapy 中使用 selenium 驱动程序单击第二个链接(使用 python)

转载 作者:太空宇宙 更新时间:2023-11-04 05:15:38 28 4
gpt4 key购买 nike

我想使用 selenium 驱动程序使用 python 单击下面 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">&nbsp;</a>
</td>
<td class="personName">
<a href="#!search/profile/person?personId=-1454220127&amp;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&amp;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">&nbsp;</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">&nbsp;</a>
</td>
<td class="personName">
<a href="#!search/profile/person?personId=-1832984054&amp;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&amp;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">&nbsp;</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 SmithTesla EVs .我试着点击Gary Smith使用下面的代码

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, "personName"))).click()

但什么也没发生。而且我也不知道如何使用 selenium 驱动程序来单击 Tesla EVs

我想说的是,随着输入的不同,内容名称会动态变化。所以准确地说,我想单击第一个和第二个链接,而内部内容名称是随机的

任何能给我提示的人都将不胜感激。

最佳答案

要点击 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()

关于jquery - 如何在 scrapy 中使用 selenium 驱动程序单击第二个链接(使用 python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41780106/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com