Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 492
justin_sakong Avatar asked Sep 11 '18 09:09

justin_sakong


People also ask

How do you read a JavaScript variable in Selenium Webdriver?

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.

Can we use XPath in JavaScriptExecutor?

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.


Video Answer


2 Answers

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")
like image 108
Andersson Avatar answered Oct 29 '22 15:10

Andersson


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.

like image 23
cruisepandey Avatar answered Oct 29 '22 15:10

cruisepandey