1

I need to change the style, background-color attribute from (213, 171, 128):

<div class="rAgBzQ" style="background-color: rgb(213, 171, 128);">
</div>

to (0, 0,0)

<div class="rAgBzQ" style="background-color: rgb(0, 0, 0);">
</div>

Tried using the following code, with the error below. Not sure what I'm doing wrong?

div_to_change = driver.find_element_by_xpath("""myxpath""")
driver.execute_script("arguments[0].style.background-color = 'rgb(0, 0, 0)';", div_to_change)

ERROR:

selenium.common.exceptions.JavascriptException: Message: javascript error: Invalid left-hand side in assignment

2 Answers 2

2

Try this

You need to set the attribute value.

div_to_change = driver.find_element_by_xpath("""myxpath""") 
driver.execute_script("arguments[0].setAttribute('style', 'background-color: rgb(0, 0, 0);')", div_to_change) 
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know if there's a way to 'save' this change afterwards? i.e. Instead of Refreshing the page and getting back to the original color. Or would the only option be to use selenium to manually click the changes?
Since you will be doing this on a browser, its just client side and will be lost once your refresh/close browser. If you want to save the page directly from script you can try content = driver.page_source with open('/pathto/webpage.html', 'w') as f: f.write(content)
0

I believe you would want to try:

div_to_change = driver.find_element_by_xpath("""myxpath""") 
div_to_change.setAttribute('background-color', 'rgb(0, 0, 0)')

1 Comment

I also believe the snipped would be "less fragile" if you found the element by using: find_element_by_class_name('rAgBzQ')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.