Search code examples
pythonseleniumweb-scrapingiframewebdriverwait

Nosuch element exception in selenium but the element is present there


I was trying to scrape some data from a site.I'm using a selenium for it but i am getting NoSuchElementException when i try to click or get data from the elements in the site even though the element present in there.

code


    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get('https://www.priceking.com/')
    driver.maximize_window()
    driver.find_element(By.XPATH, "/html/body/div/center/font/a").click()
    # driver.find_element(By.CSS_SELECTOR, "body > div > center > font > a").click()

Here is site link that i was using priceking

i don't know why i can't locate any of the elements in the site.


Solution

  • The element Show More is in an iframe. Need to switch to frame to interact with the element.

    # Imports required
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://www.priceking.com/")
    
    wait = WebDriverWait(driver,30)
    
    # Switch to Iframe
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"contents")))
    
    show_more = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[contains(text(),'Show More')]")))
    show_more.click()
    
    # Can also use Link text to click on the element.
    # show_more = wait.until(EC.element_to_be_clickable((By.LINK_TEXT,"Show More")))
    # show_more.click()
    
    # switch to default content
    driver.switch_to.default_content()