Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change HTML attribute that contain hyphen

I want to change value of table header's aria-label attribute using execute_script

HTML of target element:

<th class="sorting" tabindex="0" rowspan="1" colspan="1" aria-label="Activate to sort column ascending">Company</th>

So I try following:

driver.execute_script('document.getElementsByTagName("th")[1].aria-label="Activate to sort column descending";')

but get exception:

WebDriverException: Message: unknown error: Runtime.evaluate threw exception: ReferenceError: Invalid left-hand side in assignment

If to use

driver.execute_script('document.getElementsByTagName("th")[1].["aria-label"]="Activate to sort column descending";')

got

WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token 

So what is the correct statement to set aria-label new value?

like image 544
Andersson Avatar asked Mar 12 '23 03:03

Andersson


1 Answers

Since aria-label is an attribute, you should use .setAttribute(...) :

driver.execute_script("document.getElementsByTagName('th')[1].setAttribute('aria-label', 'Activate to sort column descending');")
like image 178
Florent B. Avatar answered Mar 20 '23 23:03

Florent B.