脚本-Python
硒自动化
尝试按降序对Web元素列表进行排序,代码已成功执行,但未执行排序操作
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
#driver.delete_all_cookies()
driver.maximize_window()
driver.get('https://jqueryui.com/sortable/')
time.sleep(5)
driver.switch_to.frame(0)
action = ActionChains(driver)
item =driver.find_elements_by_xpath("//*[@id=\"sortable\"]/li")
i = 0
for j in i <=len(item):
j = i+1
target =item[i]
dest = item[len(item)-1]
action.click(target).click_and_hold().move_to_element(dest).move_by_offset(0,20).release().perform()
答案 0 :(得分:0)
尝试
item = sorted(item, reverse=True)
答案 1 :(得分:0)
from selenium
import webdriver
from selenium.webdriver.common.action_chains
import ActionChains
from selenium.webdriver.common.keys
import Keys
import time
driver = webdriver.Chrome()# driver.delete_all_cookies()
driver.maximize_window()
driver.get('https://jqueryui.com/sortable/')
time.sleep(5)
driver.switch_to.frame(0)
action = ActionChains(driver)
items = driver.find_elements_by_xpath("//*[@id=\"sortable\"]/li")
we = driver.find_element_by_xpath("//*[@id=\"sortable\"]/li[1]")
we1 = driver.find_element_by_xpath("//*[@id=\"sortable\"]/li[7]")
print(len(items))
print(type(items))## for j in reversed(items): #print(j.text)# i = 0
for j in reversed(items):
action.drag_and_drop(driver.find_element_by_xpath("//*[@id=\"sortable\"]/li[7]"), we).perform()
print(j.rect)
time.sleep(3)
time.sleep(2)
for i in reversed(items):
action.drag_and_drop(driver.find_element_by_xpath("//*[@id=\"sortable\"]/li[7]"), we1).perform()
print(i)
time.sleep(2)
driver.quit()