第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從標(biāo)簽python的onclick屬性獲取URL

從標(biāo)簽python的onclick屬性獲取URL

富國滬深 2022-06-28 18:07:44
我正在嘗試使用 selenium python 訪問標(biāo)簽的 onclick 屬性中存在的 URL。它存在于 javascript 函數(shù)中。我已經(jīng)嘗試了各種技術(shù)來做到這一點(diǎn),但我還沒有找到解決方案。我嘗試使用 execute_script 方法執(zhí)行單擊功能。我也嘗試過 get_attribute 來獲取 onclick 函數(shù),但它沒有返回。我想訪問 openPopUpFullScreen 函數(shù)中的 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.這是另一個(gè):id=browser.find_element_by_css_selector(".openNewPage").get_attribute("onclick")print(id)#it returns none我需要 openPopUpFullScreen 函數(shù)中存在的 URL,但我無法弄清楚完成此操作的正確解決方案是什么。更新:我也嘗試使用 beautifulsoup 來提取 onclick 函數(shù),但它似乎沒有出現(xiàn):這是我的代碼: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 thisres = soup.find("a",{"class":"openNewPage"})[onclick]#it returns an error NameError: name 'onclick' is not defined
查看完整描述

2 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

以下


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


查看完整回答
反對(duì) 回復(fù) 2022-06-28
?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

對(duì)于 get_attribute:


我認(rèn)為您在沒有“onclick”屬性的情況下得到了錯(cuò)誤的元素。


您應(yīng)該擴(kuò)展 css 選擇器并確認(rèn)它只會(huì)找到一個(gè)元素。


對(duì)于 current_url:


您應(yīng)該先切換到新窗口。嘗試使用以下代碼:


# window_handles[-1] refer to last window created.

browser.switch_to.window(browser.window_handles[-1])

print(browser.current_url)   


查看完整回答
反對(duì) 回復(fù) 2022-06-28
  • 2 回答
  • 0 關(guān)注
  • 274 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)