最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python?

programmeradmin1浏览0评论

I'm trying to crawl the pages that I interested in. For this, I need to remove attribute of element from HTML. 'style' is what I want to remove. So I find some codes from Stackoverflow.(i'm using Chrome for driver)

element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)

What does arguments[0] do in the code? Can anyone explain arguments[0]'s roles concretely?

I'm trying to crawl the pages that I interested in. For this, I need to remove attribute of element from HTML. 'style' is what I want to remove. So I find some codes from Stackoverflow.(i'm using Chrome for driver)

element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)

What does arguments[0] do in the code? Can anyone explain arguments[0]'s roles concretely?

Share Improve this question edited Sep 11, 2018 at 16:20 undetected Selenium 193k44 gold badges303 silver badges378 bronze badges asked Sep 11, 2018 at 9:53 justin_sakongjustin_sakong 2892 gold badges6 silver badges13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

arguments is what you're passing from Python to JavaScript that you want to execute.

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

means that you want to "replace" arguments[0] with WebElement stored in element variable.

This is the same as if you defined that element in JavaScript:

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

You can also pass more arguments as

driver.execute_script("arguments[0].removeAttribute(arguments[1])", element, "style")
element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")  

Here, element is a web element.

and in this call:

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

You are passing element(Which is a web element) as a arguments[0]

removeAttribute('style') must be a method in JS. and using arguments[0] you are invoking this method.

As per the documentation execute_script() method synchronously executes JavaScript in the current window/frame and is defined as:

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.
  • As per the example you have provided:

    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') : Refers to the script to be executed synchronously by execute_script() method where:

    • arguments[] would be the reference of the element which will be passed through *args
    • removeAttribute() is the method to be executed.
    • style is the attribute on which the method removeAttribute() would be invoked.
  • element is the reference of the WebElement which is passed to arguments[0]

You can find a relevant discussion in What does arguments[0] and arguments[1] mean when using executeScript method from JavascriptExecutor interface through Selenium WebDriver?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论