我正在尝试使用 selenium python 访问标签的 onclick 属性中存在的 URL。它存在于 javascript 函数中。我已经尝试了各种技术来做到这一点,但我还没有找到解决方案。我尝试使用 execute_script 方法执行单击功能。我也尝试过 get_attribute 来获取 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 函数中存在的 URL,但我无法弄清楚完成此操作的正确解决方案是什么。
更新:我也尝试使用 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
慕森卡
繁华开满天机
相关分类