Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...