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?
Ans:
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")
WebElement
to be passed to theexecute_script
method. Passing the result fromelement(by.css(...))
directly causes CallStackSize error, that's why one has to useelement(by.css(...)).getWebElement()