【问题标题】:Python Selenium ExceptionPython 硒异常
【发布时间】: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()

【问题讨论】:

    标签: python selenium exception


    【解决方案1】:

    NoSuchElementException 指示您尝试在 xpathlink_text 部分中访问的元素之一。

    您可能拼错了元素或输入错误...我的建议是双重/三次检查元素(xpathlink_text 方法)。

    祝你有美好的一天,如果成功了,请告诉我!

    【讨论】:

    • 它会找到它,但如果我说没问题,那就别想了。我的意思是:寻找点击链接->没有链接(抛出异常)->通过单击下一页处理它(程序结束,但我希望它返回循环)->返回循环并一直持续到最后
    • 也许这样:如果异常:self.driver.find_element_by_xpath("/html/body/form/div[2]/div[1]/div/div[2]/div/div[ 2]/a[1]").click()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多