从标记python的onclick属性获取URL

2024-05-13 01:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用seleniumpython访问标记的onclick属性中存在的URL。它存在于javascript函数中。我试过各种方法来做到这一点,但我还没有找到解决办法。 我尝试过使用execute\u脚本方法执行on click函数。我也尝试过使用get\u属性来获取onclick函数,但它不返回任何值。 我想访问openPopUpFullScreen函数中的url

以下是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>

下面是python代码:

url=browser.find_element_by_xpath("//img[@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']").click()
print(browser.current_url)
#it returns the previous page I am at.

还有一个:

id=browser.find_element_by_css_selector(".openNewPage").get_attribute("onclick")
print(id)
#it returns none

我需要在openPopUpFullScreen函数的网址,但我不能找出什么是正确的解决方案来完成这一点。你知道吗

更新:我也尝试过使用beautifulsoup提取onclick函数,但似乎没有出现:

这是我的密码:

content = browser.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content,"html.parser")
res = soup.find("a",{"class":"openNewPage"})
print(res)
#it returns the complete tag but it does not contain onclick attribute
#i tried using this
res = soup.find("a",{"class":"openNewPage"})[onclick]
#it returns an error NameError: name 'onclick' is not defined

Tags: oftheto函数newtitleitopen
2条回答

下面

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

对于get\u属性:

我认为没有'onclick'属性的元素是错误的。你知道吗

您应该扩展css选择器并确认它将只找到一个元素。你知道吗


对于当前url:

你应该先切换到新窗口。 尝试使用以下代码:

# window_handles[-1] refer to last window created.
browser.switch_to.window(browser.window_handles[-1])
print(browser.current_url)   

相关问题 更多 >