1

I have this script that goes to google takeout and downloads the takeout file. The issue I am having is, some accounts require a relogin when the "Takeout_dlbtn" is clicked and others can download directly. I tried to put this IF ELSE statement. Where if "password" is displayed, we run through the steps to re-enter password and then download. And if the "password" does not appear, we assume we do not need to login and can download right away.

The issue I am having is, my IF statement runs, and if the password page is shown, it will enter password and then download. But if the password page is not shown, the IF statement is still run and my ELSE statement does not get executed. It throws this error "no such element :Unable to locate element: {"Method":" css select","selector":"[name-"password"]"}"

How do I get the ELSE function to run if the "password" element is not shown? Any help would be appreciated.

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")
  
Dilshan
  • 187
  • 6
  • 2
    Does this answer your question? [Checking if element exists with Python Selenium](https://stackoverflow.com/questions/9567069/checking-if-element-exists-with-python-selenium) – Payam Dec 28 '20 at 20:06

1 Answers1

0

according to the Selenium docs:

4.2. Locating by Name

Use this when you know the name attribute of an element. With this strategy, the first element with a matching name attribute will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.

So instead of if/else, try a try/catch block:

try:
    self.driver.find_element_by_name("password")
    print ("if statement")
    ...

except NoSuchElementException:
    print ("else statement")
    ...
mconkin
  • 121
  • 7
  • 2
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section *Answer Well-Asked Questions*, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Dec 28 '20 at 20:08