请告诉我如何缩短硒的python代码



我从python开始,并不是所有的基础知识这是我的脚本,它检索了一些文本,然后转到下一页,我想让它对每个人来说都更可读。我想做循环和条件我不知道在这种情况下如何使用该类,我有small(<driver.execute_script(quot;window.scrollTo(02520("(>(

driver = webdriver.Chrome('./chromedriver_win32/chromedriver')
url = driver.get("https://www.linternaute.fr/dictionnaire/fr/abecedaire/a/1/")
time.sleep(5)
driver.execute_script("window.scrollTo(0, 220);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 1320);")
time.sleep(3)
driver.execute_script("window.scrollTo(0, 2720);")
time.sleep(1)
driver.execute_script("window.scrollTo(0, 4220);")
time.sleep(2.5)
Mot_Commencent_Par_A = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul')
Mot_Commencent_Par_A = Mot_Commencent_Par_A.text.split('n')
time.sleep(2.5)
Page_2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[2]/a')
page_2 = Page_2.click()
time.sleep(4)
driver.execute_script("window.scrollTo(0, 1020);")
time.sleep(2)
Mot_Commencent_Par_AA = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AA = Mot_Commencent_Par_AA.text.split('n')
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 2520);")
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 3520);")
time.sleep(1)
driver.execute_script("window.scrollTo(0, 4220);")
Mot_Commencent_Par_AB = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[2]')
Mot_Commencent_Par_AB = Mot_Commencent_Par_AB.text.split('n')
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 6520);")
time.sleep(1)
Page_3 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[3]/a')
page_3 = Page_3.click()
time.sleep(3)
driver.execute_script("window.scrollTo(0, 1220);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 2520);")
time.sleep(2.5)
Mot_Commencent_Par_AB2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AB2 = Mot_Commencent_Par_AB2.text.split('n')
##################################""" LETTRE AB2 RECUPERER"""##########################
time.sleep(2)
driver.execute_script("window.scrollTo(0, 3220);")
time.sleep(2)
##################################""" ON COMMENCE A RECUPERER AC """##########################
driver.execute_script("window.scrollTo(0, 4520);")
time.sleep(2.5)
Mot_Commencent_Par_AC = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[2]')
Mot_Commencent_Par_AC = Mot_Commencent_Par_AC.text.split('n')
##################################""" LETTRE AC RECUPERER"""##########################
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 6520);")
time.sleep(1)
##################################""" ON PASS A LA PAGE 4"""##########################
Page_4 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[3]/a')
page_4 = Page_4.click()
time.sleep(3)
##################################""" ON SE FAIT PASSER POUR UN HUMAIN(ON SCROLL petit à petit)"""##########################
driver.execute_script("window.scrollTo(0, 1720);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 3720);")
##################################""" ON COMMENCE A RECUPERER AC2 """##########################
time.sleep(1)
driver.execute_script("window.scrollTo(0, 4220);")
time.sleep(2.5)
Mot_Commencent_Par_AC2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AC2 = Mot_Commencent_Par_AC2.text.split('n')
##################################""" LETTRE AC2 RECUPERER"""##########################
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 6520);")
time.sleep(1)
##################################""" ON PASS A LA PAGE 5"""##########################
Page_5 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[3]/a')
page_5 = Page_5.click()
time.sleep(3)
##################################""" ON SE FAIT PASSER POUR UN HUMAIN(ON SCROLL petit à petit)"""##########################
driver.execute_script("window.scrollTo(0, 1220);")
time.sleep(2)
##################################""" ON COMMENCE A RECUPERER AC3 """##########################
driver.execute_script("window.scrollTo(0, 2920);")
time.sleep(1.5)
Mot_Commencent_Par_AC3 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AC3 = Mot_Commencent_Par_AC3.text.split('n')
##################################""" LETTRE AC3 RECUPERER"""##########################
time.sleep(2)
driver.execute_script("window.scrollTo(0, 3220);")
time.sleep(2)
##################################""" ON COMMENCE A RECUPERER AD """##########################
driver.execute_script("window.scrollTo(0, 4520);")
time.sleep(2.5)
Mot_Commencent_Par_AD = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[2]')
Mot_Commencent_Par_AD = Mot_Commencent_Par_AD.text.split('n')
##################################""" LETTRE AD RECUPERER"""##########################
time.sleep(2.5)
driver.execute_script("window.scrollTo(0, 6520);")
time.sleep(1)
##################################""" ON PASS A LA PAGE 6"""##########################
Page_6 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[3]/a')
page_6 = Page_6.click()
time.sleep(3)
##################################""" ON SE FAIT PASSER POUR UN HUMAIN(ON SCROLL petit à petit)"""##########################
driver.execute_script("window.scrollTo(0, 1720);")
time.sleep(2)
##################################""" ON COMMENCE A RECUPERER AE """##########################
driver.execute_script("window.scrollTo(0, 2720);")
time.sleep(1)
Mot_Commencent_Par_AD2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AD2 = Mot_Commencent_Par_AD2.text.split('n')
driver.execute_script("window.scrollTo(0, 4520);")
time.sleep(2.5)
Mot_Commencent_Par_AE = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[2]')
Mot_Commencent_Par_AE = Mot_Commencent_Par_AE.text.split('n')
##################################""" LETTRE AE RECUPERER"""##########################
time.sleep(1.5)
driver.execute_script("window.scrollTo(0, 4520);")
time.sleep(1)
##################################""" ON PASS A LA PAGE 7"""##########################
Page_7 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/div[3]/ul/li[3]/a')
page_7 = Page_7.click()
time.sleep(3)
##################################""" ON SE FAIT PASSER POUR UN HUMAIN(ON SCROLL petit à petit)"""##########################
driver.execute_script("window.scrollTo(0, 1720);")
time.sleep(2)
##################################""" ON COMMENCE A RECUPERER AE2 """##########################
driver.execute_script("window.scrollTo(0, 2720);")
time.sleep(1)
Mot_Commencent_Par_AE2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul[1]')
Mot_Commencent_Par_AE2 = Mot_Commencent_Par_AE2.text.split('n')

我想知道如何使更短

让我们从挤压这个部分开始:

time.sleep(5)
driver.execute_script("window.scrollTo(0, 220);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 1320);")
time.sleep(3)
driver.execute_script("window.scrollTo(0, 2720);")
time.sleep(1)
driver.execute_script("window.scrollTo(0, 4220);")
time.sleep(2.5)

删除它,添加from selenium.webdriver import ActionChains并保留此:

Mot_Commencent_Par_A = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[2]/div[1]/div/div/section[3]/ul')
ActionChains(driver).move_to_element(Mot_Commencent_Par_A).perform()

对其他WebElement执行相同操作。如果ActionChains不起作用,请将这些scrollTo合并为一个。

最新更新