1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Muazam
  • 51
  • 6

1 Answers1

0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank You Sir it Works perfectly!! Just a quick question do we need to turn off frame mode ? I think I heard something like that somewhere – Muazam Feb 08 '22 at 19:00
  • @Muazam After the operations within the frame, you need to switch back [Selenium](https://stackoverflow.com/a/54482491/7429447)'s focus to the default content using `driver.switch_to.default_content()` – undetected Selenium Feb 08 '22 at 19:13