通过 Selenium 和 Python 通过 WebDriver 实例调用 execute_script() 方法时,arguments[0] 是什么?

jus*_*ong 3 javascript python selenium webdriver selenium-webdriver

我正在尝试抓取我感兴趣的页面。为此,我需要从 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)
Run Code Online (Sandbox Code Playgroud)

代码中的arguments[0] 有什么作用?谁能具体解释arguments[0]的作用?

And*_*son 5

arguments是您从 Python传递给要执行的JavaScript 的内容。

driver.execute_script("arguments[0].removeAttribute('style')", element) 
Run Code Online (Sandbox Code Playgroud)

意味着您想arguments[0]用存储在element变量中的WebElement “替换” 。

这与您在 JavaScript 中定义该元素是一样的:

driver.execute_script("document.querySelector('select.m-tcol-c#searchBy').removeAttribute('style')")
Run Code Online (Sandbox Code Playgroud)

您还可以传递更多参数作为

driver.execute_script("arguments[0].removeAttribute(arguments[1])", element, "style")
Run Code Online (Sandbox Code Playgroud)