Home > database >  How to access the iframe by XPath for Web scraping using Selenium with Python
How to access the iframe by XPath for Web scraping using Selenium with Python

Time:01-01

I only find this possible xPath, because the IDs change when refreshing the page.

It is body that gets an error when executing the code:

/html/body/div[3]/div[2]/div[2]/div[2]/div/div/div/div[1]/div/div/div/body"/table/tbody/tr[2]/td/iframe

Iframe image:

IMAGE

CodePudding user response:

you can use this snipet:

from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://webscraper.io/test-sites/e-commerce/scroll')
shop_elems = driver.find_elements_by_xpath("//div[@class='thumbnail']")

or this snipet:

from selenium.webdriver.common.by import By
shop_elems[0].get_element(By.XPATH, <XPath Query Here>)
shop_elems[0].find_element(By.XPATH, ".//a")

for more refernce you can visit here:https://towardsdatascience.com/xpath-for-python-89f4423415e0

CodePudding user response:

To switch to the iframe so you have to:

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

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://webapp']")))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://webapp')]")))
      
  • 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
    
  • Related