king-of-everything
king-of-everything

Reputation: 57

Change style with Selenium Python?

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

Upvotes: 1

Views: 619

Answers (2)

rustycodecs
rustycodecs

Reputation: 37

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

Upvotes: 0

LeelaPrasad
LeelaPrasad

Reputation: 466

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) 

Upvotes: 2

Related Questions