Search code examples
python-3.xseleniumselenium-webdriverxpathwebdriverwait

Extracting a part of message from Yopmail using Python Selenium


I want to Extract a Specific part in Python Selenium. I have done it with Pyautogui but I want to do it without that is it possible?

https://yopmail.com/     

put in inbox tab

jenniferwilks09182

I want to extract exactly this code it is in a separate div enter image description here


Solution

  • The element with the code is within an <iframe> so you have to:

    • Induce WebDriverWait for the desired frame to be available and switch to it.

    • Induce WebDriverWait for the desired element to be visible.

    • You can use either of the following Locator Strategies:

      • Using XPATH and following:

        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ifmail']")))
        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[.='Your code is:']//following::div[1]"))).text)
        
      • Using XPATH and following-sibling:

        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ifmail']")))
        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[.='Your code is:']//following-sibling::div[1]"))).text)
        
    • Note : You have to add the following imports :

       from selenium.webdriver.support.ui import WebDriverWait
       from selenium.webdriver.common.by import By
       from selenium.webdriver.support import expected_conditions as EC
      

    Reference

    You can find a couple of relevant discussions in: