【问题标题】:Delete line if the xpath if xpath can't be found in Python如果 xpath 在 Python 中找不到 xpath,则删除行
【发布时间】:2021-07-16 07:34:36
【问题描述】:

我正在使用一个使用 selenium 的 python 脚本来检查 url 列表并在那里执行操作。

现在我想,如果找不到 xpath,它会从我的 txt 文件中删除该特定行。我该怎么做?

这是我的代码

def post(status):
        with open("fbgroepen.txt") as listOfGroups:
            for group in listOfGroups:
                driver.get(group)
                time.sleep(5)
                try:
                    if status:
                        #try posting
                        try:
                            postClick = WebDriverWait(driver, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[4]/div/div/div/div/div[1]/div[1]/div/div/div/div[1]/div/div[1]/span"))).click()
                            #typ bericht
                            post_box = WebDriverWait(driver, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div/div/div/div"))).send_keys(message)
                            #click op post
                            post_element = WebDriverWait(driver, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[3]/div[2]/div/div/div"))).click()
                            time.sleep(2)
                            #wacht tot gepost
                            WebDriverWait(driver, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[4]/div/div/div/div/div[1]/div[1]/div/div/div/div[1]/div/div[1]/span")))

                        #continue to next url if xpath is'nt found
                        except:
                            continue
                    else:
                        continue
                except selenium.common.exceptions.NoSuchElementException:
                    continue 

提前致谢

【问题讨论】:

    标签: python selenium selenium-webdriver xpath selenium-chromedriver


    【解决方案1】:

    不确定是否正确理解,但如果您假设:

    #continue to next url if xpath is'nt found
    except:
        continue
    

    这部分捕获表示未找到 xpath 的情况的异常(imo 异常太宽泛,但现在让我们离开它)您可以打开另一个文件,例如

    open("fbgroepen.tmp")
    

    并将找到的每一行以及脚本末尾的内容写入它:

    import shutil
    

    def post(状态):

    tmp_file = open("fbgroepen.tmp", "w")
    
    with open("fbgroepen.txt") as listOfGroups:
        for group in listOfGroups:
            driver.get(group)
            time.sleep(5)
            try:
                if status:
                    #try posting
                    try:
                        postClick = WebDriverWait(driver, 20).until(
                        EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[4]/div/div/div/div/div[1]/div[1]/div/div/div/div[1]/div/div[1]/span"))).click()
                        #typ bericht
                        post_box = WebDriverWait(driver, 20).until(
                        EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div/div/div/div"))).send_keys(message)
                        #click op post
                        post_element = WebDriverWait(driver, 20).until(
                        EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[3]/div[2]/div/div/div"))).click()
                        time.sleep(2)
                        #wacht tot gepost
                        WebDriverWait(driver, 20).until(
                        EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[4]/div/div/div/div/div[1]/div[1]/div/div/div/div[1]/div/div[1]/span")))
                        
                        tmp_file.write(group)
                        
                    #continue to next url if xpath is'nt found
                    except:
                        continue
                else:
                    continue
            except selenium.common.exceptions.NoSuchElementException:
                continue 
                
    tmp_file.close()
    shutil.copyfile("fbgroepen.tmp", "fbgroepen.txt")
            
    

    【讨论】:

    • 但是我们怎样才能自动删除没有xpaths的url呢? @PiWo
    • @Teun 我不知道 url 存储在哪里或如何存储 - 您写道:“现在我希望如果找不到 xpath,它会从我的 txt 文件中删除该特定行。”所以我假设它在文件“fbgroepen.txt”中。在建议的解决方案中,此脚本将在写入临时文件之前对 Excelption 进行处理(除了:继续)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2015-06-30
    • 1970-01-01
    • 2020-11-15
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多