问题描述
我目前正在编写用于Web自动化的脚本,但我希望程序为我编辑一个元素,这是我正在使用的代码的示例
final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]")
final.click()
现在它不会单击它,因为该网站将元素设置为false,所以我希望硒进行编辑并将其设置为true以便可以单击它,这是有可能的,如果有的话,任何人都可以解释和感谢!
这里的HTML元素是什么样子
button class="btn btn-disabled" type="submit" data-testid="ChangeNameButton" aria-describedby="changeNameFormError" aria-disabled="true" data-bi-type="button">Change profile name
因此,我希望它不是禁用btn而是将其更改为启用,然后单击它,并在此先感谢您提交的任何帮助:)
解决方法
要更改元素的文本,请执行以下操作:
driver.execute_script("document.getElementById('theelement').innerHTML = 'changed text';");
要更改或设置元素的属性,请执行以下操作:
element = driver.find_element_by_class_name("NAMEOFELEMENT");
driver.execute_script("arguments[0].setAttribute('color: blue;')",element);
,
您尝试单击的按钮已禁用,您可以使用JS启用它。
类似这样的东西:
final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]")
driver.execute_script("arguments[0].setAttribute('className','btn-enabled');",final)