0

I use this code:

commit = driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div/div/form/div[1]/div[4]/button")
driver.execute_script("arguments[0].setAttribute('enabled', true)", commit)

But the attribute does not change.

I guess I'm using the .execute_script() function incorrectly.

I can change this attribute manually and it will give the result I want. Shown in screenshots: enter image description here enter image description here

How can i change this attribute with Selenium?

P.S. XPATH is correct.

0

2 Answers 2

1

This helped in this case:

commit = self.driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div/div/form/div[1]/div[4]/button")
self.driver.execute_script("arguments[0].disabled = false", commit)
Sign up to request clarification or add additional context in comments.

Comments

0

There are few things to notice.

  1. driver.find_element(by=By.XPATH should be

    driver.find_element(By.XPATH, "XPath here")
    
  2. Do not use absolute xpath, instead use relative path.

    //button[@type='submit' and contains(@class,'gtm_reg_btn') and @disabled]
    

You will have to make sure that, it in unique in HTMLDOM.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Bit of change you will have to make here:

Instead of this:

driver.execute_script("arguments[0].setAttribute('enabled', true)", commit)

Use this:

driver.execute_script("arguments[0].setAttribute('disabled','enabled')", commit)

3 Comments

Dont work. In my situation I need to use exactly absolute xpath.
Well, if you read the answer carefully I have mentioned that the XPath should be unique in nature.
It's not about that. Moreover, I have already answered my own question.