【发布时间】:2021-08-20 00:04:48
【问题描述】:
你能帮我写代码吗,因为我不知道如何处理这个问题。所以我想在某个网站上自动化一项任务。我正在制作的 BOT 应该执行几次点击。问题是一些链接在下一页上,我得到了 NoSuchElementException。我在 TRY EXCEPT 中关闭了我的代码,但我不知道如何让程序更进一步,并且在处理异常后不停止。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import NoSuchElementException
import os
class Bot:
def __init__(self,username,pw):
#os.environ['MOZ_HEADLESS'] = '1'
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get("https://app.fakturownia.pl/login")
sleep(1)
self.driver.find_element_by_xpath("//input[@name='user_session[login]']").send_keys(username)
self.driver.find_element_by_xpath("//input[@name='user_session[password]']").send_keys(pw)
self.driver.find_element_by_xpath("//input[@name='commit']").click()
sleep(1)
self.driver.get("url")
sleep(1)
self.driver.find_element_by_xpath("/html/body/form/div[2]/div[1]/div/div[2]/div/div[2]/a[1]").click()
def fakturuj(self):
try:
for faktura in range(2,28):
#nr fv
self.driver.find_element_by_link_text('{}/06/2021'.format(faktura + 1)).click()
#edytuj 1
self.driver.find_element_by_xpath('//*[@id="pad-wrapper"]/div[4]/div/nav/div[2]/ul[2]/li[3]/a/span').click()
#edytuj 2
self.driver.find_element_by_xpath('//*[@id="seller_text"]/label/span').click()
#odśwież nr konta
self.driver.find_element_by_xpath('//*[@id="update_seller_bank_account_button"]').click()
#zapisz
self.driver.find_element_by_xpath('//*[@id="invoice_submit_button1"]').click()
#
self.driver.find_element_by_xpath('//*[@id="pad-wrapper"]/div[4]/div/nav/div[2]/ul[1]/li[1]/a').click()
except NoSuchElementException:
#this clicks on the next page button to find the missing element but idk how to make it go back to the loop
self.driver.find_element_by_xpath(
"/html/body/form/div[2]/div[1]/div/div[2]/div/div[2]/a[1]").click()
bot = Bot('mail', 'passw')
bot.fakturuj()
【问题讨论】: