selenium——更改页面元素,让不能输入的框体变成可以输入
直接上代码吧
import unittest
import time
from selenium import webdriver
def addAttribute(driver, elementJSPath, attributeName, value):
jsScript = "document.querySelector('{0}').{1} = '{2}'".format(
elementJSPath, attributeName, value)
driver.execute_script(jsScript)
def setAttribute(driver, elementJSPath, attributeName, value):
jsScript = "document.querySelector('{0}').setAttribute('{1}', '{2}')".format(
elementJSPath, attributeName, value)
driver.execute_script(jsScript)
def removeAttribute(driver, elementJSPath, attributeName):
jsScript = "document.querySelector('{0}').removeAttribute('{1}')".format(
elementJSPath, attributeName)
driver.execute_script(jsScript)
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_dateChange(self):
url = "https://www.12306.cn/index/"
self.driver.get(url)
time.sleep(5)
elementJS = '#train_date'
removeAttribute(self.driver, elementJS, 'readonly')
element = self.driver.find_element_by_xpath('//*[@id="train_date"]')
element.clear()
element.send_keys('fuck it')
print(element.get_attribute('value'))
def tearDown(self):
time.sleep(3)
self.driver.quit()
if __name__ == "__main__":
unittest.main()
这个应该可以扩展,修改页面元素的代码,感觉挺好的
厉害了,这个可以放在代码块里使用吗
bandeng
灰色的也可以这样实现吗?我要试一下
shafa