Skip to content Skip to sidebar Skip to footer

What Is Arguments[0] While Invoking Execute_script() Method Through Webdriver Instance Through Selenium And Python?

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 Stackover

Solution 1:

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")

Solution 2:

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.

Solution 3:

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 currentwindow/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?

Post a Comment for "What Is Arguments[0] While Invoking Execute_script() Method Through Webdriver Instance Through Selenium And Python?"