python-selenium提供的execute_script调用js操作

自动化编程里会遇到定位下拉框里的元素,

#用js选择所属机构
self.driver.find_elements_by_xpath('//div[2]/div/div/div/div/i')[0].click()  #先点击下拉按钮
org = self.driver.find_element_by_xpath('//div[2]/div/div/div[2]/ul[2]/li[7]')  #定位要选择的元素,注意这里用的是find_element
#scrollIntoView()是DOM API的一部分,需要运行它WebElement但不能在列表的WebElement(S),所以不能用find_elements
self.driver.execute_script("arguments[0].scrollIntoView();", org)  #执行js,使滚动条下滑至要定位的元素位置
time.sleep(3)   #等待几秒查看滚动条是否下滑
org.click()     #点击操作

time.sleep(3)

猜你喜欢

转载自www.cnblogs.com/wenm1128/p/11534887.html
今日推荐