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 badges3 Answers
Reset to default 12arguments
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 byexecute_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 methodremoveAttribute()
would be invoked.
element
is the reference of the WebElement which is passed toarguments[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?