2 回答
TA贡献1806条经验 获得超8个赞
以下
from bs4 import BeautifulSoup
html = '''<td class="tdAction">
<div class="formResponseBtn icon-only">
<a href="#fh" onclick="javascript: openPopUpFullScreen('/esop/toolkit/negotiation/rfq/publicRfqSummaryReport.do?rfqId=rfq_229969', '');" class="openNewPage" title="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions">
<img src="/esop_custom/images/buttons/print_button.png" title="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions" alt="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions"><img src="/esop_custom/images/buttons/openNewWindow_button.png" title="(Opens in new window)" alt="(Opens in new window)">
</a>
</div>
</td>'''
soup = BeautifulSoup(html, features="lxml")
a = soup.find('a')
onclick = a.attrs['onclick']
left = onclick.find("'")
right = onclick.find("'",left+1)
print('URL is: {}'.format(onclick[left+1:right]))
输出
URL is: /esop/toolkit/negotiation/rfq/publicRfqSummaryReport.do?rfqId=rfq_229969
TA贡献1816条经验 获得超4个赞
对于 get_attribute:
我认为您在没有“onclick”属性的情况下得到了错误的元素。
您应该扩展 css 选择器并确认它只会找到一个元素。
对于 current_url:
您应该先切换到新窗口。尝试使用以下代码:
# window_handles[-1] refer to last window created.
browser.switch_to.window(browser.window_handles[-1])
print(browser.current_url)
添加回答
举报