1

I currently am writing a script for web automation but I want the program to edit an element for me here's a sample of the code I'm using

final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]") final.click()

now it won't click it since the website has the element set it false so I want selenium to edit and make the element true so it can click it, is that possible by any chance and if so can anyone explain and thanks!

Heres what the elements HTML look like

button class="btn btn-disabled" type="submit" data-testid="ChangeNameButton" aria-describedby="changeNameFormError" aria-disabled="true" data-bi-type="button">Change profile name

so instead of btn disabled, I want it to change it to enabled then click it and thanks in advance for any help submitted :)

2 Answers 2

1

The button you are trying to click is disabled and you can enable it with JS.

Something like this:

final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]")
driver.execute_script("arguments[0].setAttribute('className', 'btn-enabled');", final)
2
  • final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]") driver.execute_script("arguments[0].setAttribute('className:btn btn-enabled;')", final) i used this but i got an error
    – Eslam Ali
    Commented Sep 7, 2020 at 18:24
  • @EslamAli Sorry I had a syntax problem... I fixed it try again Commented Sep 7, 2020 at 20:24
0

To change the text of an element do something like the following:

driver.execute_script("document.getElementById('theelement').innerHTML = 'changed text';");

To change or set attribute of element:

element =  driver.find_element_by_class_name("NAMEOFELEMENT"); 
driver.execute_script("arguments[0].setAttribute('color: blue;')", element);
5
  • im not changing the text of the element rather editing it for example the common scenario when a website has your password as ***** so you change the element to text so you are able to see your password is there any way i can tell selenium to do that?
    – Eslam Ali
    Commented Sep 7, 2020 at 13:31
  • Ah, you would do this second option then which is the following:
    – AyanSh
    Commented Sep 7, 2020 at 13:34
  • final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]") driver.execute_script("arguments[0].setAttribute('type: text;')", final);
    – AyanSh
    Commented Sep 7, 2020 at 13:36
  • You would have to change the text field not the button, I put the wrong element down
    – AyanSh
    Commented Sep 7, 2020 at 13:36
  • Message: javascript error: Failed to execute 'setAttribute' on 'Element': 2 arguments required, but only 1 present. i got this error when i ran this code : final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]") driver.execute_script("arguments[0].setAttribute('className:btn btn-enabled;')", final)
    – Eslam Ali
    Commented Sep 7, 2020 at 18:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.