【发布时间】:2021-04-05 13:30:27
【问题描述】:
我有这个脚本可以去谷歌外卖并下载外卖文件。我遇到的问题是,单击“Takeout_dlbtn”时某些帐户需要重新登录,而其他帐户可以直接下载。我试图把这个 IF ELSE 语句。如果显示“密码”,我们执行重新输入密码然后下载的步骤。如果没有出现“密码”,我们假设我们不需要登录,可以立即下载。
我遇到的问题是,我的 IF 语句运行,如果显示密码页面,它将输入密码然后下载。但是如果没有显示密码页面,IF 语句仍然运行并且我的 ELSE 语句不会被执行。它抛出此错误“没有这样的元素:无法找到元素:{“方法”:“css select”,“selector”:“[name-”password”]“}”
如果“密码”元素未显示,我如何让 ELSE 函数运行?任何帮助将不胜感激。
def DLTakeout(self, email, password):
self.driver.get("https://takeout.google.com/")
time.sleep(2)
self.driver.find_element_by_xpath(takeout_DLbtn).click()
time.sleep(4)
if self.driver.find_element_by_name("password").is_displayed():
print("IF statement")
time.sleep(3)
self.driver.find_element_by_name("password").clear()
time.sleep(5)
self.driver.find_element_by_name("password").send_keys(password)
time.sleep(5)
self.driver.find_element_by_xpath("//*[@id='passwordNext']/div/button/div[2]").click()
print("Downloading")
logging.info("Downloading")
time.sleep(10) #change time.sleep to a higher number when download file is in gbs #1500
self.write_yaml(email)
print("Write to yaml successfully")
logging.info("Write to yaml successfully")
print("Zip move process about to start")
logging.info("Zip move process about to start")
time.sleep(4)
else:
print("else statement")
time.sleep(5)
print("Downloading")
logging.info("Downloading")
【问题讨论】: