繁体   English   中英

如何在不知道锚标记内的文本的情况下在 python 中使用 selenium 驱动程序单击链接

[英]how to click a link using selenium driver in python while don't know the text inside anchor tag

我使用selenium+phantomJS+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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM