为了账号安全,请及时绑定邮箱和手机立即绑定

通过 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 关注
  • 274 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号