写一个字符串来定义js代码
使用execute_script()执行js代码
province_js = ''' b= document.querySelector('ul[class="ant-cascader-menu"]:nth-child(1) >li:nth-child(1)'); b.click() ''' self._driver.execute_script(province_js) city_js =''' b= document.querySelector('ul[class="ant-cascader-menu"]:nth-child(2) >li:nth-child(1)'); b.click() ''' sleep(0.5) self._driver.execute_script(city_js) area_js = ''' b= document.querySelector('ul[class="ant-cascader-menu"]:nth-child(3) >li:nth-child(1)'); b.click() ''' sleep(0.5) self._driver.execute_script(area_js)
列子:
def select_provinces(self,provinceJs,cityJs,areaJs): ''' 省市区选择框 :param provinceJs: :param cityJs: :param areaJs: :return: ''' self.execute_js(provinceJs) sleep(0.3) self.execute_js(cityJs) sleep(0.3) self.execute_js(areaJs)