因为一些input标签,具有readonly属性,我们去要去掉readonly属性,才能实现对input标签的输入;一开始的时候我在网上查了很对资料,最终实现了,不多说了,直接上代码:
#开始日期# js = 'document.getElementsByClassName("ant-calendar-picker-input ant-input")[0].removeAttribute("readOnly")'.format("//*[@id='root']/div/div[2]/div/div[2]/div/div/div/div/div[2]/div[2]/div/div/div[1]/div/form/div[2]/div/div/span/span/input") # 使用js临时移除readonly属性js = 'document.getElementsByClassName("ant-calendar-picker-input ant-input")[0].removeAttribute("readOnly")'self.driver.execute_script(js) # 执行jstime.sleep(1)self.driver.find_element(By.XPATH,"//*[@id='root']/div/div[2]/div/div[2]/div/div/div/div/div[2]/div[2]/div/div/div[1]/div/form/div[2]/div/div/span/span/input") .clear()time.sleep(1)# self.driver.find_element(By.XPATH,"//*[@id='root']/div/div[2]/div/div[2]/div/div/div/div/div[2]/div[2]/div/div/div[1]/div/form/div[2]/div/div/span/span/input").send_keys('2023-08-24 00:00:00') # 定位日期下拉框(此时的下拉框已经可以输入了) # 输入完执行点击,使数据存入# self.driver.find_elements(By.CLASS_NAME,"ant-calendar-picker-input.ant-input")[0].send_keys("2023-08-24 00:00")js_value = 'document.getElementsByClassName("ant-calendar-picker-input ant-input")[0].value="2023-08-02 00:00"'self.driver.execute_script(js_value)
在实现过程中,我遇到了一些问题,比如去掉readonly属性之后,重新赋值没有成功,后来又加了一行代码, self.driver.find_element(By.XPATH,"//*[@id='root']/div/div[2]/div/div[2]/div/div/div/div/div[2]/div[2]/div/div/div[1]/div/form/div[2]/div/div/span/span/input") .clear() 目的就是把原来的默认值删掉;发现删掉后,还是赋值失败,最终运用js代码 js_value = 'document.getElementsByClassName("ant-calendar-picker-input ant-input")[0].value="2023-08-02 00:00"'
self.driver.execute_script(js_value)赋值成功了;