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?
We can read Javascript variables with Selenium webdriver. Selenium can run Javascript commands with the help of executeScript method. The Javascript command to be executed is passed as an argument to the method. Also we have to add the statement import org.
In Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page. In case, these locators do not work you can use JavaScriptExecutor.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With