gpt4 book ai didi

javascript - 如何在 python 中使用 selenium 驱动程序单击链接而不知道 anchor 标记内的文本

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:54 26 4
gpt4 key购买 nike

我使用selenium+phantomJS+scrapy scrapy javascript 内容。我想在加载 javascript 内容后单击表中的链接。但最重要的是,随着输入内容的不同,我要点击的链接也发生了变化。

然后我想使用xpath来定位链接,但失败了。这是我编写的 html 元素和 xpath。

<div class="results searchResults" style="display: block;”>
<table cellspacing="0" id="resultGroup">
<colgroup>
<col width="4.216%">
<col width="43.13%">
<col width="52.65%">
</colgroup>
<tbody>
<tr class="">
<td class="selector">
<a href="#" id="c_2441797" title="Apple Inc." class="checkbox unchecked">&nbsp;</a>
</td>
<td class="name">
<div>
<a href="#!search/profile/company?companyId=2441797&amp;targetid=profile" class="companyResultsName">Apple Inc.</a>
<a href="http://www.google.com/finance?client=ob&amp;q=NASDAQ: AAPL" rel="external" target="_blank">(NASDAQ: AAPL)</a>
</div>

xpath 是 self.driver.find_element_by_xpath(".//*[@id='resultGroup']/tbody/tr[@class='name']/div[1]/a[@class='companyResultsName']").click()

希望有人能给我提示。谢谢。

最佳答案

如果您的元素是由 JavaScript 动态生成的,您应该等到它出现在 DOM 中:

 from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "companyResultsName")))
link.click()

关于javascript - 如何在 python 中使用 selenium 驱动程序单击链接而不知道 anchor 标记内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758059/

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