如果在Python中找不到xpath,请删除第行

2024-06-16 12:59:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个使用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 

提前谢谢


Tags: todivbyhtmldriverbodyelementbe
1条回答
网友
1楼 · 发布于 2024-06-16 12:59:42

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

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

这一部分捕获表示找不到xpath的情况的异常(在我看来,异常范围太广,但暂时不谈),您可以打开另一个文件,例如

open("fbgroepen.tmp")

然后把找到的每一行都写下来,在脚本的末尾写下这样的内容:

import shutil

def员额(状态):

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")
        

相关问题 更多 >