7

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
justin_sakong
  • 249
  • 1
  • 6
  • 12

3 Answers3

11

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")
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Note the word `WebElement` to be passed to the `execute_script`method. Passing the result from `element(by.css(...))` directly causes CallStackSize error, that's why one has to use `element(by.css(...)).getWebElement()` – Capricorn Jun 03 '21 at 12:57
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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
2

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352