亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Selenium 和 Python 通過 WebDriver 實例調用

通過 Selenium 和 Python 通過 WebDriver 實例調用

喵喔喔 2021-06-03 14:58:33
我正在嘗試抓取我感興趣的頁面。為此,我需要從 HTML 中刪除元素的屬性。'style' 是我想要刪除的。所以我從 Stackoverflow 中找到了一些代碼。(我使用 Chrome 作為驅動程序)element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")driver.execute_script("arguments[0].removeAttribute('style')", element)代碼中的arguments[0] 有什么作用?誰能具體解釋arguments[0]的作用?
查看完整描述

2 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

arguments是您從 Python傳遞給要執行的JavaScript 的內容。

driver.execute_script("arguments[0].removeAttribute('style')", element)

意味著您想arguments[0]用存儲在element變量中的WebElement “替換” 。

這與您在 JavaScript 中定義該元素是一樣的:

driver.execute_script("document.querySelector('select.m-tcol-c#searchBy').removeAttribute('style')")

您還可以傳遞更多參數作為

driver.execute_script("arguments[0].removeAttribute(arguments[1])", element, "style")


查看完整回答
反對 回復 2021-06-15
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

根據文檔execute_script()方法在當前窗口/框架中同步執行JavaScript并定義為:

execute_script(script, *args)

    Synchronously Executes JavaScript in the current window/frame.

    Where:

        script: The JavaScript to execute.

        *args: Any applicable arguments for your JavaScript.

根據您提供的示例:


element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")

driver.execute_script("arguments[0].removeAttribute('style')", element)

arguments[0].removeAttribute('style'): 指要通過execute_script()方法同步執行的腳本,其中:


arguments[] 將是將通過的元素的引用 *args

removeAttribute() 是要執行的方法。

style是removeAttribute()將調用該方法的屬性。

element是傳遞給的WebElement的引用arguments[0]


查看完整回答
反對 回復 2021-06-15
  • 2 回答
  • 0 關注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號