我正在尝试在python中使用selenium来点击下面Annual Report
提取中的html
。
但是,driver.find_element_by_link_text("Annual Reports").click()
似乎不起作用。我得到的错误消息是Unable to locate element:
我也尝试过:
for i in driver.find_elements_by_class_name('url'):
print(i.get_attribute('href'))
但这会返回None
我应该怎么做才能让selenium点击此链接?
<li class="sic_fundamental selected">
<a href="/fundamental/factsheet.html">Fundamental</a>
<ul>
<li>
<a href="/fundamental/events_calendar.html">
<span class="url">Events Calendar</span>
<div class="description">Compilation of Corporate Actions like Dividend, Rights and Bonus Issues and Financial Result release dates. Keep abreast of stock events for the day and react accordingly to different events.</div>
</a>
<div class="subNav"><a href="/fundamental/events_calendar.html#/?type=events_upcoming" class="first-child">Upcoming</a><a href="/fundamental/events_calendar.html#/?type=events_exdates">Ex-Dates</a><a href="/fundamental/events_calendar.html#/?type=events_results">Results Release</a></div>
</li>
<li>
<a href="/fundamental/dividend_analysis.html">
<span class="url">Dividend Analysis</span>
<div class="description">Research the best high yield stocks by analysing the dividend history of each company and gain new insights into the dividend trend using our financial charts.</div>
</a>
<div class="subNav"></div>
</li>
<li>
<a href="/fundamental/annual_reports.html">
<span class="url">Annual Reports</span>
<div class="description">Browse through our complete catalog of Annual Reports for listed companies</div>
</a>
<div class="subNav"></div>
</li>
<li>
<a href="/fundamental/stocks_comparison.html">
<span class="url">Stocks Comparison</span>
<div class="description">Do a comparison of different stocks to find out which one is better. Compare the fundamentals and financials of different stocks.</div>
</a>
<div class="subNav"><a href="/fundamental/stocks_comparison.html#/?type=compare_stocks" class="first-child">Compare Stocks</a><a href="/fundamental/stocks_comparison.html#/?type=compare_warrants">Compare Warrants</a></div>
</li>
</ul>
</li>
答案 0 :(得分:0)
实际链接文本不是“年度报告”,而是“年度报告” 浏览我们的上市公司年度报告的完整目录“,因此您可以尝试仅通过部分文本匹配所需的链接:
driver.find_element_by_partial_link_text("Annual Reports").click()
如果你仍然得到Unable to locate element
,请尝试等待一段时间让所需元素显示在DOM
中,因为它可能会以恐怖方式生成:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(elemenet_to_be_clickable((By.PARTIAL_LINK_TEXT, "Annual Reports"))).click()