我使用的是一个使用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
提前感谢
不确定是否正确理解,但如果您假设:
#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")